Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I count 23 in a PEG language without repetition. Here's an expanded version of your fragment that runs successfully:

    # from http://news.ycombinator.com/item?id=404609
    bar = 3
    bat = 4
    baz = 5
    puts(("foo #{bar} %s %s" % [bat, baz]) * 2.+(0b0010))
And here's a grammar for the subset of Ruby it uses:

    this <- 'A parsing expression grammar for a tiny subset of Ruby.'.
    copyright <- 'Written by Kragen Javier Sitaker 2008; in the public domain.'.

    program  <- _ expr program / .
    noninfix <- inparens / call / constant / quasiquoted / list / name.
    expr     <- infix / noninfix.
    inparens <- '('_ expr ')'_.

    call <- name '('_ commalist ')'_ / constant '.'_ methodname '('_ commalist ')'_.

    constant <- '0b' bits / digits.
    bits     <- bit bits / bit _.
    bit      <- '0' / '1'.
    digits   <- digit digits / digit _.
    digit    <- bit / '2' / '3' / '4' / '5' / '6' / '7' / '8' / '9'.

    quasiquoted   <- '"' qqcontents '"'_.
    qqcontents    <- !'#{' !'"' char qqcontents / interpolation qqcontents / .
    interpolation <- '#{' expr '}'.

    list      <- '['_ commalist ']'_.
    commalist <- expr ','_ commalist / expr / .

    infix <- noninfix '%'_ expr / noninfix '*'_ expr / noninfix '='_ expr.

    _  <- sp _ / .
    sp <- ' ' / '\n' / comment.

    comment      <- '#' commentchars '\n'.
    commentchars <- !'\n' char commentchars / .

    namechar <- 'b' / 'a' / 'r' / 't' / 'z' / 'p' / 'u' / 's'.
    name     <- namechar name / namechar _.

    methodname <- '+'_ / name.
I have tested it (in http://github.com/kragen/peg-bootstrap/tree/master/peg.md) and it seems to work for that fragment, although I haven't examined the parse tree to see if it's right. (Obviously the definition of namechar is wrong, and there's no $end token separating statements, and you can't call methods on things that aren't constants or self, etc.)


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: