Sorting out Parboiled issues. Initial parser draft complete.

* Created test script.
* Created working parser.
This commit is contained in:
Jonathan Bernard 2011-08-25 17:08:55 -05:00
parent 303f8839fd
commit e8ebcd4998
8 changed files with 87 additions and 32 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/
.*.sw?

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,21 @@
import com.jdblabs.jlp.JLPPegParser
import org.parboiled.Parboiled
import org.parboiled.parserunners.ReportingParseRunner
import org.parboiled.parserunners.RecoveringParseRunner
parser = Parboiled.createParser(JLPPegParser.class)
parseRunner = new RecoveringParseRunner(parser.CodePage())
testLine = """%% This the first test line.
%% Second Line
%% Third Line
Fourth line
%% Fifth line
%% @author Sixth Line
%% @Example Seventh Line
%% Markdown lines (eigth line)
%% Still markdown (ninth line)
Tenth line is a code line
"""
result = parseRunner.run(testLine)

View File

@ -1,19 +0,0 @@
package com.jdblabs.jlp
@BuildParseTree
public class JLPPegParser extends BaseParser<Object> {
Rule CodePage() {
return ZeroOrMore(FirstOf(
DocBlock(),
CodeBlock())) }
Rule DocBlock() {
return OneOrMore(FirstOf(
DirectiveBlock(),
MarkdownBlock())) }
Rule DirectiveBlock() {
return FirstOf(
Sequence("%% "
}

View File

@ -2,7 +2,7 @@ package com.jdblabs.jlp
public class JLPMain { public class JLPMain {
public static void main(String args[]) { public static void main(String[] args) {
JLPMain inst = new JLPMain() JLPMain inst = new JLPMain()
@ -25,7 +25,7 @@ public class JLPMain {
// get files passed in // get files passed in
def filenames = opts.getArgs() def filenames = opts.getArgs()
def files = filenames.collect { new File(if) } def files = filenames.collect { new File(it) }
// -------- parse input -------- // // -------- parse input -------- //
files.inject(documentContext) { docContext, file -> files.inject(documentContext) { docContext, file ->
@ -35,17 +35,6 @@ public class JLPMain {
} }
public void parse(File inputFile, Map docCtx) { public void parse(File inputFile, Map docCtx) {
def currentDocBlock
def thisDoc = [ blocks:[] ]
String docName = inputFile.name.substring(
0, inputFile.name.lastIndexOf('.'))
docCtx.docs[docName] = thisDoc
inputFile.eachLine { line, lineNum ->
}
} }
} }

View File

@ -0,0 +1,62 @@
package com.jdblabs.jlp
import org.parboiled.BaseParser
import org.parboiled.Rule
import org.parboiled.annotations.*
public class JLPPegParser extends BaseParser<Object> {
public Rule CodePage() {
println "Parsing CodePage"
ZeroOrMore(FirstOf(
DocBlock(),
CodeBlock())) }
Rule DocBlock() {
OneOrMore(FirstOf(
DirectiveBlock(),
MarkdownBlock())) }
Rule CodeBlock() {
OneOrMore(Sequence(
TestNot(DOC_START), RemainingLine())) }
Rule DirectiveBlock() {
FirstOf(
// there is a bug in parboiled that prevents sequences of greater
// than 2, so this ia workaround
Sequence(
Sequence(
Sequence(DOC_START, DIRECTIVE_START),
Sequence(LongDirective(), RemainingLine())),
Sequence(Optional(MarkdownBlock()))),
Sequence(
Sequence(DOC_START, DIRECTIVE_START),
Sequence(LineDirective(), RemainingLine()))) }
Rule LongDirective() { FirstOf(AUTHOR_DIR, DOC_DIR, EXAMPLE_DIR) }
Rule LineDirective() { ORG_DIR }
Rule MarkdownBlock() { OneOrMore(MarkdownLine()) }
Rule MarkdownLine() {
Sequence(DOC_START, Sequence(TestNot(DIRECTIVE_START), RemainingLine())) }
Rule RemainingLine() { Sequence(OneOrMore(NOT_EOL), EOL) }
Rule DOC_START = String("%% ")
Rule EOL = Ch('\n' as char)
Rule NOT_EOL = Sequence(TestNot(EOL), ANY)
Rule DIRECTIVE_START= Ch('@' as char)
Rule SLASH = Ch('/' as char)
// directive terminals
Rule AUTHOR_DIR = IgnoreCase("author")
Rule DOC_DIR = IgnoreCase("doc")
Rule EXAMPLE_DIR = IgnoreCase("example")
Rule ORG_DIR = IgnoreCase("org")
}