Git Product home page Git Product logo

Comments (7)

igordejanovic avatar igordejanovic commented on July 19, 2024

Yes, it is intended behaviour although not yet documented as it seems.

parglare will not assume by default that the whole input should be consumed. If you want to do that use EOF rule at the end:

g = Grammar.from_string('S: T EOF; T: NL* L+ NL* | NL*; L: /.+/; NL: "\n";', re_flags=re.MULTILINE)

I'll close this issue when the docs get fixed.

from parglare.

igordejanovic avatar igordejanovic commented on July 19, 2024

Documented here

from parglare.

amerlyq avatar amerlyq commented on July 19, 2024

By the way, your solution above results into irritating None at the end of the ast with one more nesting level, i.e.:

g = Grammar.from_string('S: T EOF; T: NL* L+[NL] NL* | NL*; terminals L: /.+/; NL: "\n";', re_flags=re.MULTILINE)
print(Parser(g, ws=False).parse('\nL1\nL2\n\n'))
# [[['\n'], ['L1', 'L2'], ['\n', '\n']], None]

g = Grammar.from_string('S: L* EOF; terminals L: /.*/;')
print(Parser(g).parse('\nL1\nL2\n\n'))
# [['L1', 'L2'], None]

Is it intended? How to get rid of that None? Or has it some useful purpose?

from parglare.

igordejanovic avatar igordejanovic commented on July 19, 2024

Each rule, when reduced, will either call registered action or the default action. The default action for non-terminals is to create a list of elements where each element correspond to RHS element. I decided not to introduce any implicit removal, for the user to be sure how many elements will be in the list, so EOF also produces a result which is None. If you are using the default action, like in the example above, be sure to unpack your result:

result, _ = Parser(g).parse('...')

or

result = Parser(g).parse('...')[:-1]

If you prefer to get a clean result from the parse call than register your actions, at least for the root rule.
There is a built-in action pass_single which will work nice in your example:

g = Grammar.from_string('@pass_single S: L* EOF; terminals L: /.*/;')
print(Parser(g).parse('\nL1\nL2\n\n'))
# ['L1', 'L2']

from parglare.

igordejanovic avatar igordejanovic commented on July 19, 2024

As a side note, I think that a special built-in actions strip_first and strip_last could be added as that feels to be a frequent requirement. In that case strip_last would be a more general approach as it would work in the case when you have more than one rule before EOF. I'm registering issue #63 for tracking this.

from parglare.

amerlyq avatar amerlyq commented on July 19, 2024

By the way, I found only now, but the very first example which started this whole issue works as I had expected at the very beginning, i.e. parses till the very end. Isn't it regression comparing to what you described earlier and in the current docs?
http://www.igordejanovic.net/parglare/stable/grammar_language/#special-built-in-rules
I expected that same snippet will continue to work same way, but now it produces error as I wanted...

g = Grammar.from_string('T: NL* L+ NL* | NL*; L: /.+/; NL: "\n";', re_flags=re.MULTILINE)
print(Parser(g, ws=False).parse('\nL1\nL2\n\n'))
# ParseError: Error at 3:0:"\nL1\n*L2\n\n" => Expected: STOP but found <L(L2)>

I re-tried that example to be sure that using @pass_single won't discard EOF directive at the end and it will continue to produce errors instead of incomplete parsing. So, as I couldn't get partial parsing anymore, I hope you reassure me that @pass_single won't affect enforced parsing till the end and only affect resulting AST.

from parglare.

igordejanovic avatar igordejanovic commented on July 19, 2024

Seems that your are right. It looks like a regression. The old behavior should be the correct one. Will investigate.

Yes, actions are not affecting parsing in any way, they are affecting just the resulting output. In the future there might be a support for side-effects and interplay between actions, recognizers and dynamic disambiguation which will bring a possibility to affect the parsing course from different parts of user written code.

from parglare.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.