13 Commits (e510ecc3d01103b61b504128cee5341dd8c4e005)

Author SHA1 Message Date
David Majda e510ecc3d0 Switch from first/rest to head/tail in example grammars
In the past year I worked on various grammars where first/rest or
head/tail were used as labels for parts of lists. I found I associate
head/tail with a list immediately, while in case of first/rest I have to
"parse" grammar rules for a while before understanding their structure.

Moreover, I tend to assume that rest is a list of the same thigs as
first, but I don't have such assumption in case of head/tail. This
assumption was in conflict with the grammar structure.

I'm not sure how much these observations are applicable to others, but I
decided to act on them and switch from first/rest to head/tail.
9 years ago
David Majda 10d7a6aded Simplify the arithmetics example grammar
The arithmetics example grammar is the first thing everyone sees in the
online editor at the PEG.js website, but it begins with a complicated
|combine| function in the initializer. Without understanding it it is
impossible to understand code in the actions. This may be a barrier to
learning how PEG.js works.

This commit removes the |combine| function and gets rid of the whole
initializer, removing the learning obstacle and streamlining action
code. The only cost is a slight code duplication.
9 years ago
David Majda 9e8cb04c81 examples/arithmetics.pegjs: Remove trailing whitespace 9 years ago
chunpu e6efe09ac3 Update example arithmetics.pegjs, make it work 10 years ago
David Majda f5443d2bf1 Complete rewrite of the arithmetics example grammar
This is a complete rewrite of the arithmetics example grammar. It now
allows whitespace between tokens, supports "-" and "/" operators, and
gets the operator associativity right. Also, rule names now match the usual
conventions (term, factor,...).

Beside this, the rewrite reflects how I write grammars today (as opposed
to few years ago) and what style I would recommend to others.
10 years ago
David Majda 9ec6b6aa57 Text nodes: Use text nodes in examples/arithmetics.pegjs 12 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 8f2aa1e834 Fixed a bug in the example grammar where integers beginning with 0 were parsed as octal. 14 years ago
David Majda aff85fd228 Removed subtraction and division from the example grammar because of wrong associativity. 14 years ago
David Majda 9d239db2d6 Simplified the example arithmetics grammar once more + addded comment. 14 years ago
David Majda 1ee0049b08 Changed standard grammar extension from .peg to .pegjs (it is more specific). 14 years ago