From 13e0e72fed82449d89ded08cf1bf1c1c8343ddc2 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 26 Aug 2011 05:51:29 -0500 Subject: [PATCH] Fixed parser weirdness. More readable parser finished. For whatever reason, writing the parser in Groovy was causing weird errors to occur when the parser or parse runner was created. Using a plain Java source file fixed this. --- src/main/com/jdblabs/jlp/JLPPegParser.groovy | 62 ------------------- src/main/com/jdblabs/jlp/JLPPegParser.java | 58 +++++++++++++++++ src/test/TestParser.class | Bin 0 -> 1952 bytes src/test/TestParser.java | 27 ++++++++ 4 files changed, 85 insertions(+), 62 deletions(-) delete mode 100644 src/main/com/jdblabs/jlp/JLPPegParser.groovy create mode 100644 src/main/com/jdblabs/jlp/JLPPegParser.java create mode 100644 src/test/TestParser.class create mode 100644 src/test/TestParser.java diff --git a/src/main/com/jdblabs/jlp/JLPPegParser.groovy b/src/main/com/jdblabs/jlp/JLPPegParser.groovy deleted file mode 100644 index 0cec44b..0000000 --- a/src/main/com/jdblabs/jlp/JLPPegParser.groovy +++ /dev/null @@ -1,62 +0,0 @@ -package com.jdblabs.jlp - -import org.parboiled.BaseParser -import org.parboiled.Rule -import org.parboiled.annotations.* - -public class JLPPegParser extends BaseParser { - - 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") - -} diff --git a/src/main/com/jdblabs/jlp/JLPPegParser.java b/src/main/com/jdblabs/jlp/JLPPegParser.java new file mode 100644 index 0000000..bf4a7fa --- /dev/null +++ b/src/main/com/jdblabs/jlp/JLPPegParser.java @@ -0,0 +1,58 @@ +package com.jdblabs.jlp; + +import org.parboiled.Action; +import org.parboiled.BaseParser; +import org.parboiled.Context; +import org.parboiled.Rule; +import org.parboiled.annotations.*; + +public class JLPPegParser extends BaseParser { + + public Rule CodePage() { + return ZeroOrMore(FirstOf( + DocBlock(), + CodeBlock())); } + + Rule DocBlock() { + return OneOrMore(FirstOf( + DirectiveBlock(), + MarkdownBlock())); } + + Rule CodeBlock() { + return OneOrMore(Sequence( + TestNot(DOC_START), RemainingLine())); } + + Rule DirectiveBlock() { + return FirstOf( + + // there is a bug in parboiled that prevents sequences of greater + // than 2, so this ia workaround + Sequence(DOC_START, DIRECTIVE_START, LongDirective(), + RemainingLine(), Optional(MarkdownBlock())), + + Sequence(DOC_START, DIRECTIVE_START, LineDirective(), + RemainingLine())); } + + Rule LongDirective() { return FirstOf(AUTHOR_DIR, DOC_DIR, EXAMPLE_DIR); } + + Rule LineDirective() { return ORG_DIR; } + + Rule MarkdownBlock() { return OneOrMore(MarkdownLine()); } + + Rule MarkdownLine() { + return Sequence(DOC_START, TestNot(DIRECTIVE_START), RemainingLine()); } + + Rule RemainingLine() { return Sequence(OneOrMore(NOT_EOL), EOL); } + + Rule DOC_START = String("%% "); + Rule EOL = Ch('\n'); + Rule NOT_EOL = Sequence(TestNot(EOL), ANY); + Rule DIRECTIVE_START= Ch('@'); + Rule SLASH = Ch('/'); + + // directive terminals + Rule AUTHOR_DIR = IgnoreCase("author"); + Rule DOC_DIR = IgnoreCase("doc"); + Rule EXAMPLE_DIR = IgnoreCase("example"); + Rule ORG_DIR = IgnoreCase("org"); +} diff --git a/src/test/TestParser.class b/src/test/TestParser.class new file mode 100644 index 0000000000000000000000000000000000000000..24b1c47ad2638af1da66ecc46ee7231fcd0546db GIT binary patch literal 1952 zcma)6Yjaao6kR82dy;SqC8aL`DT0~?+9`9!+PwGk2fYKKrb-U;q63$6o+$z)j%=ypci@ZyNEI5pPSp zW5%0C+%n=_iF^vHcu!)@tlv-J6mBQ5ZpMuy3b>QR2e>QoVG3zu?<3qZv-=5rY{tz5 ziYb(^C9y3qv1&VZuqF`CEbR)672JI-aAwnXbSZ3BwYQ_H4ep4Fz-(q|)Aj1Pmh!5u z-O&5Fa@f#$9;9sDHrm`KQ`*8D6eXnv2<paR=ibAJT6$cj71AC z8Swz5Hq|5CL1f{IIs6o#S=htpMAri(@zBB-_);J@=u>)^^g_qco}Vl0mg@!dUGz4= zbJxNpWA!|~qOVkyUrkkO7QV(e63RjqHG0#+KInqE9;QwZA|~3C_|`%l2NrA`GIA*k zkI)cEl-$Ll3Tg+8hJ_{=%K1Tjy$}dYj|j5BmB9;s*fJz?h60(CwI4PD-i?siZf={o z5fGeK%>f^F@RH+}qqn@GYw{tJIUe>Ck}^@z--X(#@m6uft>bq3xpXt?1k^3 zoNzoh$e+AB|@w^O& zu8a&2ZTIDKK{^9Ezb3$kf4NAEMAnCw>v-O&GvHdXW>up2D9$(D(K ziG9axMu<_y5MKBw5>C7nG zyKDwY_HXk|K$!kOd6BD&90kYm>=DGToCv(wRRU66v6?B3>P;jf)x~T!+eXZYcpGD5 ze`0(u&c#Hf4atpPhOA6-&0f^CatX1>ip)-_HWI_iQ&eWpI<8zDp?sFg>|w{1Ya^7? z^uSfT+*SS=ag6c${b?lQWbhcNzmeR0f>V3f+OUo=S-hS;-Nu>HZUg(pbw z9btO;G0wFylb)q$W9}!6r{~*PFwTob&YQ(J=QB8sS^9jQ { + + public Rule S() { + return OneOrMore( + Sequence(A(), OneOrMore(B()), C())); } + + Rule A() { return Ch('a'); } + Rule B() { return Ch('b'); } + Rule C() { return Ch('c'); } + + public static void main(String[] args) { + TestParser parser = Parboiled.createParser(TestParser.class); + ReportingParseRunner parseRunner = new ReportingParseRunner(parser.S()); + + ParsingResult result = parseRunner.run("abbbbcabc"); + System.out.println(result.matched ? printNodeTree(result) + "\n" : "No Match"); + } +}