Quantcast

Why isn't an Action legal as the first rule of a SequenceMatcher?

classic Classic list List threaded Threaded
2 messages Options
fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Why isn't an Action legal as the first rule of a SequenceMatcher?

fge
Right now, I am pruning the code of grappa for v2 (by the way, 1.0.0 is out!), and I am removing dead code; but there is one method I can't get rid of in Context, and that is setIntTag.

The only call to it I have left is in SequenceMatcher:

<code>
    @Override
    public <V> boolean match(final MatcherContext<V> context)
    {
        final Object valueStackSnapshot = context.getValueStack()
            .takeSnapshot();

        final List<Matcher> children = getChildren();
        final int size = children.size();
        for (int i = 0; i < size; i++) {
            final Matcher matcher = children.get(i);

            // remember the current index in the context, so we can access it
            // for building the current follower set
            // TODO: removing this makes a parse run fail!
            context.setIntTag(i); // HERE

            if (!matcher.getSubContext(context).runMatcher()) {
                // rule failed, so invalidate all stack actions the rule might
                // have done
                context.getValueStack().restoreSnapshot(valueStackSnapshot);
                return false;
            }
        }
        context.createNode();
        return true;
    }
</code>

When I remove this particular line from SequenceMatcher, I get this  stack trace:

org.testng.TestNGException:
Cannot instantiate class com.github.parboiled1.grappa.parsingresult.PrevCallsParsingResultTest
        at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
        at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
        at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
        at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:115)
        at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:200)
        at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
        at org.testng.TestRunner.initMethods(TestRunner.java:409)
        at org.testng.TestRunner.init(TestRunner.java:235)
        at org.testng.TestRunner.init(TestRunner.java:205)
        at org.testng.TestRunner.<init>(TestRunner.java:160)
        at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
        at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
        at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:575)
        at org.testng.SuiteRunner.init(SuiteRunner.java:159)
        at org.testng.SuiteRunner.<init>(SuiteRunner.java:113)
        at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
        at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
        at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
        ... 27 more
Caused by: org.parboiled.errors.ParserRuntimeException: Error while parsing action 'mainRule/digits/digits2/digits2_Action1' at input position\norg.parboiled.errors.GrammarException: Illegal call to getMatch(), getMatchStartIndex(), getMatchEndIndex() or getMatchRange(), only valid in Sequence rule actions that are not in first position (line 1, pos 4):
100+23
   ^

        at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:510)
        at com.github.parboiled1.grappa.matchers.delegate.SequenceMatcher.match(SequenceMatcher.java:62)
        at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
        at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
        at com.github.parboiled1.grappa.matchers.delegate.SequenceMatcher.match(SequenceMatcher.java:62)
        at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
        at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
        at com.github.parboiled1.grappa.matchers.delegate.SequenceMatcher.match(SequenceMatcher.java:62)
        at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
        at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
        at org.parboiled.parserunners.BasicParseRunner.run(BasicParseRunner.java:57)
        at org.parboiled.parserunners.ReportingParseRunner.runBasicMatch(ReportingParseRunner.java:80)
        at org.parboiled.parserunners.ReportingParseRunner.run(ReportingParseRunner.java:52)
        at com.github.parboiled1.grappa.parsingresult.ParsingResultTest.<init>(ParsingResultTest.java:67)
        at com.github.parboiled1.grappa.parsingresult.PrevCallsParsingResultTest.<init>(PrevCallsParsingResultTest.java:29)
        ... 32 more
Caused by: org.parboiled.errors.GrammarException: Illegal call to getMatch(), getMatchStartIndex(), getMatchEndIndex() or getMatchRange(), only valid in Sequence rule actions that are not in first position
        at org.parboiled.support.Checks.ensure(Checks.java:54)
        at org.parboiled.DefaultMatcherContext.checkActionContext(DefaultMatcherContext.java:372)
        at org.parboiled.DefaultMatcherContext.getMatch(DefaultMatcherContext.java:306)
        at org.parboiled.BaseActions.match(BaseActions.java:83)
        at com.github.parboiled1.grappa.testparsers.Action$J1H71H8RUPMG11P2.run(Unknown Source)
        at com.github.parboiled1.grappa.matchers.ActionMatcher.match(ActionMatcher.java:127)
        at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
        at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
        ... 46 more

The key here is, I believe, in the last stack trace: "only valid in Sequence rule actions that are not in first position".

Why?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why isn't an Action legal as the first rule of a SequenceMatcher?

mathias
Administrator
The semantics of getMatch(), getMatchStartIndex(), getMatchEndIndex() or getMatchRange() are to
query a property of the immediately preceding match.
If you put `getMatchStartIndex()` into the very first place in a Sequence then there is not immediately preceding match and the action makes no sense.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 7.1.2015, at 05:00, fge [via parboiled users] <[hidden email]> wrote:

> Right now, I am pruning the code of grappa for v2 (by the way, 1.0.0 is out!), and I am removing dead code; but there is one method I can't get rid of in Context, and that is setIntTag.
>
> The only call to it I have left is in SequenceMatcher:
>
> <code>
>     @Override
>     public <V> boolean match(final MatcherContext<V> context)
>     {
>         final Object valueStackSnapshot = context.getValueStack()
>             .takeSnapshot();
>
>         final List<Matcher> children = getChildren();
>         final int size = children.size();
>         for (int i = 0; i < size; i++) {
>             final Matcher matcher = children.get(i);
>
>             // remember the current index in the context, so we can access it
>             // for building the current follower set
>             // TODO: removing this makes a parse run fail!
>             context.setIntTag(i); // HERE
>
>             if (!matcher.getSubContext(context).runMatcher()) {
>                 // rule failed, so invalidate all stack actions the rule might
>                 // have done
>                 context.getValueStack().restoreSnapshot(valueStackSnapshot);
>                 return false;
>             }
>         }
>         context.createNode();
>         return true;
>     }
> </code>
>
> When I remove this particular line from SequenceMatcher, I get this  stack trace:
>
> org.testng.TestNGException:
> Cannot instantiate class com.github.parboiled1.grappa.parsingresult.PrevCallsParsingResultTest
>         at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
>         at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
>         at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
>         at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:115)
>         at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:200)
>         at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
>         at org.testng.TestRunner.initMethods(TestRunner.java:409)
>         at org.testng.TestRunner.init(TestRunner.java:235)
>         at org.testng.TestRunner.init(TestRunner.java:205)
>         at org.testng.TestRunner.<init>(TestRunner.java:160)
>         at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
>         at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
>         at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:575)
>         at org.testng.SuiteRunner.init(SuiteRunner.java:159)
>         at org.testng.SuiteRunner.<init>(SuiteRunner.java:113)
>         at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
>         at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
>         at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
>         at org.testng.TestNG.run(TestNG.java:1057)
>         at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
>         at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
>         at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
>         at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:606)
>         at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
> Caused by: java.lang.reflect.InvocationTargetException
>         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
>         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>         at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
>         at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
>         ... 27 more
> Caused by: org.parboiled.errors.ParserRuntimeException: Error while parsing action 'mainRule/digits/digits2/digits2_Action1' at input position\norg.parboiled.errors.GrammarException: Illegal call to getMatch(), getMatchStartIndex(), getMatchEndIndex() or getMatchRange(), only valid in Sequence rule actions that are not in first position (line 1, pos 4):
> 100+23
>    ^
>
>         at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:510)
>         at com.github.parboiled1.grappa.matchers.delegate.SequenceMatcher.match(SequenceMatcher.java:62)
>         at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
>         at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
>         at com.github.parboiled1.grappa.matchers.delegate.SequenceMatcher.match(SequenceMatcher.java:62)
>         at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
>         at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
>         at com.github.parboiled1.grappa.matchers.delegate.SequenceMatcher.match(SequenceMatcher.java:62)
>         at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
>         at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
>         at org.parboiled.parserunners.BasicParseRunner.run(BasicParseRunner.java:57)
>         at org.parboiled.parserunners.ReportingParseRunner.runBasicMatch(ReportingParseRunner.java:80)
>         at org.parboiled.parserunners.ReportingParseRunner.run(ReportingParseRunner.java:52)
>         at com.github.parboiled1.grappa.parsingresult.ParsingResultTest.<init>(ParsingResultTest.java:67)
>         at com.github.parboiled1.grappa.parsingresult.PrevCallsParsingResultTest.<init>(PrevCallsParsingResultTest.java:29)
>         ... 32 more
> Caused by: org.parboiled.errors.GrammarException: Illegal call to getMatch(), getMatchStartIndex(), getMatchEndIndex() or getMatchRange(), only valid in Sequence rule actions that are not in first position
>         at org.parboiled.support.Checks.ensure(Checks.java:54)
>         at org.parboiled.DefaultMatcherContext.checkActionContext(DefaultMatcherContext.java:372)
>         at org.parboiled.DefaultMatcherContext.getMatch(DefaultMatcherContext.java:306)
>         at org.parboiled.BaseActions.match(BaseActions.java:83)
>         at com.github.parboiled1.grappa.testparsers.Action$J1H71H8RUPMG11P2.run(Unknown Source)
>         at com.github.parboiled1.grappa.matchers.ActionMatcher.match(ActionMatcher.java:127)
>         at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:64)
>         at org.parboiled.DefaultMatcherContext.runMatcher(DefaultMatcherContext.java:491)
>         ... 46 more
>
> The key here is, I believe, in the last stack trace: "only valid in Sequence rule actions that are not in first position".
>
> Why?
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Why-isn-t-an-Action-legal-as-the-first-rule-of-a-SequenceMatcher-tp4024353.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...