Giter Site home page Giter Site logo

Comments (9)

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

The last two examples should work, yes. Could you send me your file? Maybe I pushed a non working version on github?

About the silent fail, yes, you're right, I get this pb also while working on a grammar.

I'm in the middle of rewriting the parser, but the final goal is to get better errors. From there, grammar should test if Grammar.parse(input) failed or not. Sorry for the discomfiture.

from pegged.

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

Ok, I think I get it: a char class does not accept '-' as a normal char. When you wrote [+-], it looked for another char after the - and failed. Hence the whole failure.

I used ('+' / '-') when I tried to match numbers in that way (see pegged.examples.numbers), I never thought of using a class.

Hmm, what's the regex workaround for this?

from pegged.

jminer avatar jminer commented on July 18, 2024

Ah, yes, '-' is treated specially inside a class. I didn't think about using it literally there, but that does usually work with regexes. I just tried Java's regex, and it seems like if the '-' is the first or last char in the class, it treats it normally. Both [+-] and [-+] work with it.

Here's the reduced version: https://gist.github.com/2128672 and here's the Ruby grammar I started on: https://gist.github.com/2128764

I wrote my first grammar last week. Pegged is the first parser generator I've gotten working. Thanks for writing it. I played with ANTLR some a few years ago, but had troubles learning it.

Edit: I just tried Ruby's and Notepad2's regexes, and they seem to both work like Java's.

from pegged.

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

OK, but then how does one asl for ']' in a class? I would think that there must be an escape mechanism: -, ] and \ for -, ] and \ respectively. Along with - in first or last pos.

But that will make the char class grammar a mess...

from pegged.

jminer avatar jminer commented on July 18, 2024

I just tested with regexes, and they do have escapes like you mentioned. To use a \, [, or ] in a regex char class, you have to escape them: \\, \[, and \]. (I got errors when trying to use [ unescaped.) In regexes, you can also escape other special characters like + and * even though it is unneeded.

I don't care whether you implement this or not. It would be a little more convenient, but as you say, it adds complexity. One can always work around it by using ([a-z] / '[' / ']')* instead of [a-z\[\]]*.

from pegged.

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

OK, I just implemented escape sequences for -, [, ] and . Just use -, [ ...

Btw, ' and " also work in char classes (with correct D strings that is, I like to use ` as a string delimiter.

from pegged.

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

Oh and btw, [-+] works also, because it's parsed as Char Char and not Char '-' Char

from pegged.

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

Your examples now work, either with [-+] or with [+\-].

Keep me informed on the Ruby grammar!

from pegged.

PhilippeSigaud avatar PhilippeSigaud commented on July 18, 2024

Hmm, I re-introduced space sensitivity in Pegged which will impact your Ruby grammar.
See the main wiki page, but the idea is that <- is PEG-standard space-sensitive rule (no space consumed between sequence elements), whereas using just < as an arrow (a 'space arrow') will create a space consuming rule, at least for its main constituents.

That means that for the gist you showed me, instead of :

        Comment <- '#' > (!EOL > .)* > (EOL / EOI)

    Expr <- FloatLiteral / IntegerLiteral

    # OctLiteral comes before DecLiteral
    IntegerLiteral <~ HexLiteral / BinLiteral / OctLiteral / DecLiteral
    FloatLiteral <~ DecLiteral > '.' > DecLiteral > ([eE] > [+-]? > [0-9]+)?

do:

        Comment <- '#'  (!EOL > .)*   (EOL / EOI)

    Expr <- FloatLiteral / IntegerLiteral

    # OctLiteral comes before DecLiteral
    IntegerLiteral <~ HexLiteral / BinLiteral / OctLiteral / DecLiteral
    FloatLiteral <~ DecLiteral   '.'   DecLiteral   ([eE]  [-+]?  [0-9]+)?

Which hopefully results in a cleaner grammar.

from pegged.

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.