Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Unified Diff: tools/lexer_generator/automaton.py

Issue 169523003: Experimental parser: split and rename some files (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/lexer_generator/action.py ('k') | tools/lexer_generator/code_generator.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/lexer_generator/automaton.py
diff --git a/tools/lexer_generator/automaton.py b/tools/lexer_generator/automaton.py
index 5bb71f87d7f409cd8400f08dd0d41a7f7546f8bc..c0f95cde7d6dac8b75012dde33af0c4725a01791 100644
--- a/tools/lexer_generator/automaton.py
+++ b/tools/lexer_generator/automaton.py
@@ -25,10 +25,61 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-from types import TupleType, ListType
+from types import IntType, TupleType, ListType
from itertools import chain
-from action import Term, Action
-from transition_keys import TransitionKey
+from term import Term
+from transition_key import TransitionKey
+
+class Action(object):
+
+ __empty_action = None
+
+ @staticmethod
+ def empty_action():
+ if Action.__empty_action == None:
+ Action.__empty_action = Action(Term.empty_term(), -1)
+ return Action.__empty_action
+
+ @staticmethod
+ def dominant_action(actions):
+ dominant = Action.empty_action()
+ for action in actions:
+ if not action:
+ continue
+ if not dominant:
+ dominant = action
+ continue
+ if action.precedence() == dominant.precedence():
+ assert action.__term == dominant.__term
+ elif action.precedence() < dominant.precedence():
+ dominant = action
+ return dominant
+
+ def __init__(self, term, precedence):
+ assert isinstance(term, Term)
+ assert type(precedence) == IntType
+ assert not term or precedence >= 0, 'action must have positive precedence'
+ self.__term = term
+ self.__precedence = precedence
+
+ def name(self):
+ return self.__term.name()
+
+ def term(self):
+ return self.__term
+
+ def precedence(self):
+ return self.__precedence
+
+ def __nonzero__(self):
+ 'true <==> self == empty_action'
+ return bool(self.__term)
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.__term == other.__term
+
+ def __str__(self):
+ return "action <%s>" % ('' if not self.__term else str(self.__term))
class AutomatonState(object):
'''A base class for dfa and nfa states. Immutable'''
« no previous file with comments | « tools/lexer_generator/action.py ('k') | tools/lexer_generator/code_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698