Commit graph

213 commits

Author SHA1 Message Date
David Majda ebf5d969b2 s/alwaysAdvancesOnSuccess/alwaysConsumesOnSuccess/
Matches terminology change from the previous commit.
2015-09-04 17:35:37 +02:00
David Majda 6ff005786c Talk about "consuming input", not "advancing parser position"
It's shorter, less technical, and more understandible.
2015-09-04 17:23:42 +02:00
David Majda 60ebd9e695 Simplify JSHint directives 2015-09-04 16:52:38 +02:00
David Majda 20a4fb2e7f Update version to 0.9.0 2015-08-30 08:22:26 +02:00
David Majda a4b4a57569 Custom tracers specs: s/'/"/
The convention is to use single quotes only for strings containing code.
2015-08-21 17:38:04 +02:00
David Majda 1d38b973ee Custom tracers specs: Simplify code slightly 2015-08-21 17:36:34 +02:00
David Majda 671c22e80f Avoid using |console| in default tracer and its tests when not defined
This makes default tracer and its tests work in IE 8-10.
2015-08-21 17:32:37 +02:00
David Majda e8b379f945 Rewrite |trace| option tests to avoid using |console|
This makes them cleaner and also makes them work in IE 8-10.
2015-08-21 17:32:23 +02:00
David Majda 969d39e8d9 Remove trailing commas in object literals
They broke IE 8-9.
2015-08-21 16:02:13 +02:00
David Majda a4772376fb Renumber bytecode instructions sequentially 2015-08-18 10:57:50 +02:00
David Majda ad27a300a8 Fix left recursion detection in sequences
Report left recursion also in cases where the recursive rule invocation
is not a direct element of a sequence, but is wrapped inside an
expression.

Fixes #359.
2015-08-17 10:58:37 +02:00
David Majda 703a352985 Change few testcase descriptions
Reaction to changes in 130cbcfaa3.
2015-08-06 16:38:38 +02:00
David Majda d7d7e87874 Make infinite loop and left recursion detectors work with named rules
Add missing |named| case to the visitor in lib/compiler/asts.js, which
makes the infinite loop and left recursion detectors work correctly with
named rules.

The missing case caused |make parser| to fail with:

  140:34: Infinite loop detected.
  make: *** [parser] Error 1
2015-08-06 16:32:08 +02:00
David Majda 317059760a Fix incorrect pass name in a spec description 2015-07-17 15:37:45 +02:00
David Majda 373f48c10f Fix small error in two testcases
Pointed out by @Mingun:

  6ce97457bf (commitcomment-10548605)
2015-07-17 15:37:26 +02:00
David Majda 6f2c75f7d8 Label specs: Improve structure and descriptions 2015-07-17 14:16:46 +02:00
David Majda 8487c9a0ff Label specs: Add missing test case 2015-07-17 11:43:30 +02:00
David Majda f4385da177 Label specs: Unify formatting with other code 2015-07-17 11:36:35 +02:00
David Majda ddff5feea3 Label specs: Simplify and regularize block-scoped label specs
There is no need to test 3 labels from the outside scope, 1 is enough.
2015-07-17 11:28:15 +02:00
Arlo Breault 12c169e7b5 Convert PEG.js code to strict mode
* Issues #323
2015-06-12 17:34:59 -07:00
Arlo Breault 1a32ae7bd0 Make PEG global explicit in helpers 2015-06-12 17:33:12 -07:00
David Majda eaca5f0acf Add location information to |GrammarError|
This means all errors thrown by |PEG.buildParser| now have associated
location information.
2015-04-06 17:34:37 +02:00
David Majda 89146915ce Add location information to AST nodes
This will allow to add location information to |GrammarError| exceptions
thrown in various passes.
2015-04-06 17:34:37 +02:00
David Majda d1fe86683b Improve location info in tracing events
Replace |line|, |column|, and |offset| properties of tracing events with
the |location| property. It contains an object similar to the one
returned by the |location| function available in action code:

  {
    start: { offset: 23, line: 5, column: 6 },
    end:   { offset: 25, line: 5, column: 8 }
  }

For the |rule.match| event, |start| refers to the position at the
beginning of the matched input and |end| refers to the position after
the end of the matched input.

For |rule.enter| and |rule.fail| events, both |start| and |end| refer to
the current position at the time the rule was entered.
2015-04-04 10:04:09 +02:00
David Majda 065f4e1b75 Improve location info in syntax errors
Replace |line|, |column|, and |offset| properties of |SyntaxError| with
the |location| property. It contains an object similar to the one
returned by the |location| function available in action code:

  {
    start: { offset: 23, line: 5, column: 6 },
    end:   { offset: 25, line: 5, column: 8 }
  }

For syntax errors produced in the middle of the input, |start| refers to
the first unparsed character and |end| refers to the character behind it
(meaning the span is 1 character). This corresponds to the portion of
the input in the |found| property.

For syntax errors produced the end of the input, both |start| and |end|
refer to a character past the end of the input (meaning the span is 0
characters).

For syntax errors produced by calling |expected| or |error| functions in
action code the location info is the same as the |location| function
would return.
2015-04-04 10:04:04 +02:00
David Majda b1ad2a1f61 Rename |reportedPos| to |savedPos|
Preform the following renames:

  * |reportedPos| -> |savedPos| (abstract machine variable)
  * |peg$reportedPos| -> |peg$savedPos| (variable in generated code)
  * |REPORT_SAVED_POS| -> |LOAD_SAVED_POS| (instruction)
  * |REPORT_CURR_POS| -> |UPDATE_SAVED_POS| (instruction)

The idea is that the name |reportedPos| is no longer accurate after the
|location| change (seea the previous commit) because now both
|reportedPos| and |currPos| are reported to user code. Renaming to
|savedPos| resolves this inaccuracy.

There is probably some better name for the concept than quite generic
|savedPos|, but it doesn't come to me.
2015-04-03 16:26:34 +02:00
David Majda 4f7145e360 Improve location info available in action code
Replace |line|, |column|, and |offset| functions with the |location|
function. It returns an object like this:

  {
    start: { offset: 23, line: 5, column: 6 },
    end:   { offset: 25, line: 5, column: 8 }
  }

In actions, |start| refers to the position at the beginning of action's
expression and |end| refers to the position after the end of action's
expression. This allows one to easily add location info e.g. to AST
nodes created in actions.

In predicates, both |start| and |end| refer to the current position.

Fixes #246.
2015-04-03 16:26:34 +02:00
David Majda 889563a0ae Add missing ";" 2015-04-02 14:35:06 +02:00
David Majda 3473c6cb64 Remove extra whitespace 2015-04-02 14:34:26 +02:00
David Majda fb320c4c59 Fix small errors in Jasmine matcher messages 2015-04-02 14:32:31 +02:00
David Majda d7fc0b5c3b Implement infinite loop detection
Fixes #26.
2015-04-01 12:21:43 +02:00
David Majda 6ce97457bf Fix left recursion detection
So far, left recursion detector assumed that left recursion occurs only
when the recursive rule is at the very left-hand side of rule's
expression:

  start = start

This didn't catch cases like this:

  start = "a"? start

In general, if a rule reference can be reached without consuming any
input, it can lead to left recursion. This commit fixes the detector to
consider that.

Fixes #190.
2015-04-01 10:10:51 +02:00
David Majda da57118a43 Implement basic support for tracing
Parsers can now be generated with support for tracing using the --trace
CLI option or a boolean |trace| option to |PEG.buildParser|. This makes
them trace their progress, which can be useful for debugging. Parsers
generated with tracing support are called "tracing parsers".

When a tracing parser executes, by default it traces the rules it enters
and exits by writing messages to the console. For example, a parser
built from this grammar:

  start = a / b
  a = "a"
  b = "b"

will write this to the console when parsing input "b":

  1:1 rule.enter start
  1:1 rule.enter   a
  1:1 rule.fail    a
  1:1 rule.enter   b
  1:2 rule.match   b
  1:2 rule.match start

You can customize tracing by passing a custom *tracer* to parser's
|parse| method using the |tracer| option:

  parser.parse(input, { trace: tracer });

This will replace the built-in default tracer (which writes to the
console) by the tracer you supplied.

The tracer must be an object with a |trace| method. This method is
called each time a tracing event happens. It takes one argument which is
an object describing the tracing event.

Currently, three events are supported:

  * rule.enter -- triggered when a rule is entered
  * rule.match -- triggered when a rule matches successfully
  * rule.fail  -- triggered when a rule fails to match

These events are triggered in nested pairs -- for each rule.enter event
there is a matching rule.match or rule.fail event.

The event object passed as an argument to |trace| contains these
properties:

  * type   -- event type
  * rule   -- name of the rule the event is related to
  * offset -- parse position at the time of the event
  * line   -- line at the time of the event
  * column -- column at the time of the event
  * result -- rule's match result (only for rule.match event)

The whole tracing API is somewhat experimental (which is why it isn't
documented properly yet) and I expect it will evolve over time as
experience is gained.

The default tracer is also somewhat bare-bones. I hope that PEG.js user
community will develop more sophisticated tracers over time and I'll be
able to integrate their best ideas into the default tracer.
2015-03-30 14:00:19 +02:00
David Majda fb5f6c6ee9 Make labels behave like block-scoped variables
Action and predicate code can now see variables defined in expressions
"above" them.

Based on a pull request by Bryon Vandiver (@asterick):

  https://github.com/pegjs/pegjs/pull/180

Fixes #316.
2015-02-13 14:10:32 +01:00
David Majda 73795a65cc Behavior specs cleanup: Add group specs
While groups don't create separate nodes on the AST level, they exist
as concept on the user level, so they should be specified.
2015-01-26 09:54:29 +01:00
David Majda e306b58443 Behavior specs cleanup: Improve error reporting specs 2015-01-26 09:54:29 +01:00
David Majda e9d038547d Behavior specs cleanup: Improve semantic predicate specs
Note that use of |text| inside semantic predicate code is no longer
tested and officially supported.
2015-01-26 09:54:29 +01:00
David Majda 3d9600b81b Behavior specs cleanup: Improve action specs 2015-01-26 09:54:29 +01:00
David Majda b623396cb8 Behavior specs cleanup: Improve initializer specs
Note that use of |text|, |offset|, |line|, and |column| inside
initializer code is no longer tested and officially supported.
2015-01-26 09:54:29 +01:00
David Majda fee58d0806 Behavior specs cleanup: Improve rule specs 2015-01-26 09:54:29 +01:00
David Majda fa70e70ddf Behavior specs cleanup: Improve choice specs 2015-01-24 20:04:49 +01:00
David Majda b3828919e2 Behavior specs cleanup: Improve sequence specs 2015-01-24 20:02:06 +01:00
David Majda 548209b48b Behavior specs cleanup: Improve simple predicate specs 2015-01-24 19:57:49 +01:00
David Majda 52144e48cb Behavior specs cleanup: Improve label specs 2015-01-24 19:57:49 +01:00
David Majda c5c44b9d0c Behavior specs cleanup: Improve text specs 2015-01-24 19:57:49 +01:00
David Majda bb708490a0 Behavior specs cleanup: Improve one or more specs 2015-01-24 19:57:49 +01:00
David Majda b9a3b44cf2 Behavior specs cleanup: Improve zero or more specs 2015-01-24 19:57:49 +01:00
David Majda c377eff876 Behavior specs cleanup: Improve optional specs 2015-01-24 19:57:48 +01:00
David Majda ade2c249f2 Behavior specs cleanup: Improve rule reference specs 2015-01-24 19:57:48 +01:00
David Majda 936d6453a5 Behavior specs cleanup: Improve dot specs 2015-01-24 19:57:48 +01:00