add hierarchy to "action &= WS1 & op & WS1 & tail;"

27 views
Skip to first unread message

Brannon King

unread,
Sep 17, 2013, 4:56:55 PM9/17/13
to eto-...@googlegroups.com
In this line
 
action &= WS1 & op & WS1 & tail;
 
action and op and tail are all named parsers. I really want op and tail to be children of action, not peers. How do I change the line to make that happen?
 
 

 

 

Curtis Wensley

unread,
Sep 17, 2013, 5:09:47 PM9/17/13
to eto-...@googlegroups.com
If you are giving action a name after that line, op and tail should be children when matched. You might be giving the action a name at the wrong level.

For example, if you do this:

var op = Terminals.Set("=").Named("op");
var tail = new StringParser().Named("tail");
var action = mySomething.Named("action");

action &= op & tail;

then, action, op, and tail will be peers.

If you do this instead:

Parser action = mySomething;
action &= op & tail;
action.Name = "action";

then op and tail will be children.

Curtis Wensley

unread,
Sep 17, 2013, 5:28:15 PM9/17/13
to eto-...@googlegroups.com
Some other examples:

Correct (recommended)

Parser action = something.Separate(); // doesn't create a sequence yet.  If something is a sequence, separate it so we don't add to it
action &= somethingElse; // creates a new sequence here
action &= op & tail;
action.Name = "action";

or:

var action = something & somethingElse; // creates the sequence
action.Name = "action";
action &= op & tail;

The key is to name your parsers at the end, especially when using operators.  If you're not using operators then it gets much more explicit what is getting named, and what is not.

This creates the following hierarchy:

SequenceParser (named action)
something
somethingElse
op
tail

or, to create without operators:

var action = new SequenceParser { Name = "action" };
action.Add(something);
action.Add(somethingElse);
action.Add(op);
action.Add(tail);

These are incorrect:

Parser action = (something & somethingElse).Named("action"); // Named creates a new unary parser
action &= op & tail; // creates a new sequence parser

becomes:

SequenceParser (no name)
UnaryParser (named action)
SequenceParser
something
somethingElse
op
tail

or this:

Parser action = something; // haven't created the sequence yet!
action.Name = "action"; // we're actually setting the name on 'something' here
action &= op & term; // finally, we've created the sequence.. but no name!

creates:

SequenceParser
something (named action)
op
term

Curtis.


On Tuesday, September 17, 2013 1:56:55 PM UTC-7, Brannon King wrote:
Reply all
Reply to author
Forward
0 new messages