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

Unified Diff: tools/lexer_generator/automaton.py

Issue 71313002: Experimental parser: action refactor (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month 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/automata_test.py ('k') | tools/lexer_generator/dfa.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 843cf91ee35beed2a6a5ab433619e4f15d6a2e78..1ed580c4ea75105fa47f2824d4da39fc24f474fe 100644
--- a/tools/lexer_generator/automaton.py
+++ b/tools/lexer_generator/automaton.py
@@ -30,12 +30,49 @@ from itertools import chain
from transition_keys import TransitionKey
class Action(object):
- pass
+
+ def __init__(self, type, data = None, precedence = -1):
+ assert type
+ self.__type = type
+ self.__data = data
+ self.__precedence = precedence
+
+ def type(self):
+ return self.__type
+
+ def data(self):
+ return self.__data
+
+ def precedence(self):
+ return self.__precedence
+
+ def __hash__(self):
+ return hash((self.__type, self.__data))
+
+ def __eq__(self, other):
+ return (isinstance(other, self.__class__) and
+ self.__type == other.__type and
+ self.__data == other.__data)
+
+ def __str__(self):
+ if not self.__data:
+ return "action<%s>" % self.__type
+ return "action<%s, %s>" % (self.__type, self.__data)
class AutomatonState(object):
- def __init__(self, node_number):
- self.__node_number = node_number
+ __node_number_counter = 0
+
+ def __init__(self):
+ self.__node_number = AutomatonState.__node_number_counter
+ AutomatonState.__node_number_counter += 1
+
+ def __hash__(self):
+ return hash(self.__node_number)
+
+ def __eq__(self, other):
+ return (isinstance(other, self.__class__) and
+ self.__node_number == other.__node_number)
def node_number(self):
return self.__node_number
@@ -91,16 +128,20 @@ class Automaton(object):
def to_dot(self):
def escape(v):
- v = str(v).replace('\r', '\\\\r')
- v = str(v).replace('\t', '\\\\t')
- v = str(v).replace('\n', '\\\\n')
- v = str(v).replace('\\', '\\\\')
- v = str(v).replace('\"', '\\\"')
+ v = str(v)
+ v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n')
+ v = v.replace('\\', '\\\\').replace('\"', '\\\"')
return v
def f(node, (node_content, edge_content)):
if node.action():
- action_text = node.action()[1].split('\n')[0]
+ action = node.action()
+ if action.type() == 'code':
+ # assert action.data()
+ action_text = action.data()
+ else:
+ action_text = action.type()
+ action_text = escape(action_text)
node_content.append(' S_l%s[shape = box, label="%s"];' %
(node.node_number(), action_text))
node_content.append(' S_%s -> S_l%s [arrowhead = none];' %
« no previous file with comments | « tools/lexer_generator/automata_test.py ('k') | tools/lexer_generator/dfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698