18 Commits (fba70833dd93d61cc1574b855a67ff6af71dfe40)

Author SHA1 Message Date
David Majda da9a32a5f3 Example grammars: Improve |parseInt| invocations
Instead of |parseInt("0x" + digits)| do |parseInt(digits, 16)|, which is
a bit cleaner.
11 years ago
David Majda 86769a6c5c Error handling: Make |?| return |null| on unsuccessful match
Before this commit, the |?| operator returned an empty string upon
unsuccessful match. This commit changes the returned value to |null|. It
also updates the PEG.js grammar and the example grammars, which used the
value returned by |?| quite often.

Returning |null| is possible because it no longer indicates a match
failure.

I expect that this change will simplify many real-world grammars, as an
empty string is almost never desirable as a return value (except some
lexer-level rules) and it is often translated into |null| or some other
value in action code.

Implements part of #198.
11 years ago
David Majda f3d392bd1c JavaScript example: Fix handling of elided elements in array literals
JavaScript allows one to skip (elide) elements in array literals. It
also allows a trailing comma, which doesn't imply an element elision.

For example, an array literal:

  [,,,]

contains three elided elements (one before each comma) and a trailing
comma.

Example JavaScript parser handled elided elements incorrectly and just
threw them away. This commit fixes this behvior and inserts |null| in
the AST for each elided element. This is in line with how SpiderMonkey's
JavaScript parser (the |Reflect.parse| API), Esprima and Acorn behave.

Based on a patch by @fpirsch:

  https://github.com/dmajda/pegjs/pull/177
11 years ago
David Majda d71bca46a1 Javascript example: Improve array literal rules
Makes the |ArrayLiteral| and |ElementList| rules more in line with the
ECMAScript grammar.

Based on a patch by @fpirsch:

  https://github.com/dmajda/pegjs/pull/177
11 years ago
fpirsch d7e853b87c Fix automatic semi-colon insertion
Fix automatic semi-colon insertion in var statements without
initialisers.
var i
i = 1;
is valid and not accepted by the parser

but
var i = 2
i = 3;
is valid and accepted by the parser, as it should be.

With this fix, both are accepted.
11 years ago
David Majda c54483bb17 Text nodes: Use text nodes in examples/javascript.pegjs 12 years ago
fpirsch fa05142292 Update examples/javascript.pegjs
Changed "arguments" to "args" in several places to avoid shadowing "arguments", which is not allowed by Google Clusure Compiler.
12 years ago
David Majda 70e4166bb2 Fix typo in a comment in JavaScript example grammar 12 years ago
David Majda 8ae3eea7c4 Fix typo in JavaScript example grammar
Fixes GH-62.
12 years ago
Jason Davies d386d3a351 Fix typo in comment. 13 years ago
David Majda 5cf66d824c Fix typo in JavaScript example grammar 13 years ago
David Majda 5f810f803b Make example grammars compatible with Rhino 14 years ago
David Majda ee8c121676 Use labeled expressions and variables instead of $1, $2, etc.
Labeled expressions lead to more maintainable code and also will allow
certain optimizations (we can ignore results of expressions not passed
to the actions).

This does not speed up the benchmark suite execution statistically
significantly on V8.

Detailed results (benchmark suite totals):

---------------------------------
 Test #     Before       After
---------------------------------
      1   28.43 kB/s   28.46 kB/s
      2   28.38 kB/s   28.56 kB/s
      3   28.22 kB/s   28.58 kB/s
      4   28.76 kB/s   28.55 kB/s
      5   28.57 kB/s   28.48 kB/s
---------------------------------
Average   28.47 kB/s   28.53 kB/s
---------------------------------

Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4
14 years ago
David Majda 409ddf2ae8 Formatted all grammars more consistently and transparently
This is purely cosmetical change, no functionality was affected
(hopefully).
14 years ago
David Majda 698564a3c2 Replace ":" after a rule name with "="
I'll introduce labelled expressions shortly and I want to use ":" as a
label-expression separator. This change avoids conflict between the two
meanings of ":". (What would e.g. "foo: 'bar'" mean?  Rule "foo"
matching string "bar", or string "bar" labelled "foo"?)
14 years ago
David Majda f9ea46ef15 Fix string literal parsing in the JavaScript grammar 14 years ago
David Majda 7fc491412d Work around the fact that IE does not recognize "\v" in strings. 14 years ago
David Majda 6a194e8f68 Added example JavaScript parser. 14 years ago