-
Notifications
You must be signed in to change notification settings - Fork 156
The Value Stack
During the rule execution phase your parser actions can make use of the “value stack” for organizing the construction of custom objects like AST nodes. The value stack is a simple stack construct that serves as temporary storage for custom objects.
Generally a rule can alter the value stack in any way it pleases. It can create new values and push them onto the stack, it can consume existing values, turn them into new values and push these, and so on. The parboiled parsing engine generally doesn’t care about how your rules and parser actions work with the stack.
There are two important exceptions though:
If a rule did not match for whatever reason the value stack is reset to the exact state it had before the execution of the failing rule was started. This means that failing rules cannot alter the value stack (this includes failing semantic predicate actions)!
The reason for this behavior is to enable you to place actions not only in the last position of a sequence of rules.
Consider this rule sequence:
Rule ← ‘a’ ‘b’ action ‘c’
Assume that the action run after matching ‘b’ pushes a new value onto the value stack.
If this rule sequence fails because its final element ‘c’ did not match the value stack would still contain the new value pushed by the action. Even though your rule logic might be able to deal with these cases this would greatly complicate action design and make the parser logic much more brittle.
The fact that parboiled resets the value stack in case of a rule mismatch enables you to place parser actions freely and use the value stack also for intra-Rule temporary storage, which can be really convenient in many scenarios.
Test and TestNot rules never affect the value stack. parboiled always resets the value stack back to a saved snapshot after a Test or TestNot rule has matched. You can therefore be sure that syntactic predicates will never “mess” with your value stack setup, even if they contain parser actions or reference other rules that do.
- Introduction
- ... Motivation
- ... Features
- ... Simple Java Example
- ... Simple Scala Example
- ... RegEx vs. parboiled vs. Parser Generators
- ... Projects using parboiled
- Installation
- Concepts
- ... The Big Picture
- ... The Rule Tree
- ... The Value Stack
- ... The Parse Tree
- ... AST Construction
- ... Parse Error Handling
- parboiled for Java
- ... Rule Construction in Java
- ... Parser Action Expressions
- ... Working with the Value Stack
- ... Action Variables
- ... Parser Extension in Detail
- ... Style Guide
- ... Java Examples
- ...... ABC Grammar
- ...... Calculators
- ...... Time Parser
- ...... Java Parser
- ...... Markdown processor
- parboiled for Scala
- ... Rule Construction in Scala
- ... Parser Actions in Scala
- ... Parser Testing in Scala
- ... Scala Examples
- ...... Simple Calculator
- ...... JSON Parser
- Advanced Topics
- ... Handling Whitespace
- ... Parsing Performance Tuning
- ... Indentation Based Grammars
- ... The ProfilingParseRunner
- ... Grammar and Parser Debugging
- ... Thread Safety
- Building parboiled
- parboiled-core Javadoc API
- parboiled-java Javadoc API
- parboiled-scala Scaladoc API
- Change Log
- Patch Policy