PistonDevelopers / meta

A DSL parsing library for human readable text documents
MIT License
89 stars 5 forks source link

Terminate self rule at infinite recursion #311

Closed bvssvni closed 8 years ago

bvssvni commented 8 years ago

If a node is enters itself without advancing, it risks entering an infinite recursion loop. Could detect this and terminate the self rule. This will make the parser try other rules as alternatives.

For example:

0 r = {
    [r r]
    "a"
}

Reading:

aaa

Call tree:

r
|- r (terminate)
   |- "a"
|- r
   |- r (terminate)
      |- "a"
   |- r
      |- r (terminate)
         |- "a"

This could make it easier to write advanced grammars.

bvssvni commented 8 years ago

There are 3 states, one when the rule has not been called, one when it is called, and one when it is terminated. The node gets terminated when it is called with the same TokenizerState twice.

pub enum Terminate {
    None,
    Some(usize),
    LastChance(usize),
}

The terminated state the LastChance state, where it has to find some other rule that makes progress, or else it will fail. It can not call itself because that would be giving itself another chance.

bvssvni commented 8 years ago

This will prevent infinite recursion, but won't allow left recursion because it can not backtrack once a select rule matches. It builds chains in the right argument, but often you want to build chains in the left argument.

bvssvni commented 8 years ago

Will have to wait for later.