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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/action.py ('k') | tools/lexer_generator/code_generator.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 the V8 project authors. All rights reserved. 1 # Copyright 2013 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without 2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are 3 # modification, are permitted provided that the following conditions are
4 # met: 4 # met:
5 # 5 #
6 # * Redistributions of source code must retain the above copyright 6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer. 7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above 8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following 9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided 10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution. 11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its 12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived 13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission. 14 # from this software without specific prior written permission.
15 # 15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 from types import TupleType, ListType 28 from types import IntType, TupleType, ListType
29 from itertools import chain 29 from itertools import chain
30 from action import Term, Action 30 from term import Term
31 from transition_keys import TransitionKey 31 from transition_key import TransitionKey
32
33 class Action(object):
34
35 __empty_action = None
36
37 @staticmethod
38 def empty_action():
39 if Action.__empty_action == None:
40 Action.__empty_action = Action(Term.empty_term(), -1)
41 return Action.__empty_action
42
43 @staticmethod
44 def dominant_action(actions):
45 dominant = Action.empty_action()
46 for action in actions:
47 if not action:
48 continue
49 if not dominant:
50 dominant = action
51 continue
52 if action.precedence() == dominant.precedence():
53 assert action.__term == dominant.__term
54 elif action.precedence() < dominant.precedence():
55 dominant = action
56 return dominant
57
58 def __init__(self, term, precedence):
59 assert isinstance(term, Term)
60 assert type(precedence) == IntType
61 assert not term or precedence >= 0, 'action must have positive precedence'
62 self.__term = term
63 self.__precedence = precedence
64
65 def name(self):
66 return self.__term.name()
67
68 def term(self):
69 return self.__term
70
71 def precedence(self):
72 return self.__precedence
73
74 def __nonzero__(self):
75 'true <==> self == empty_action'
76 return bool(self.__term)
77
78 def __eq__(self, other):
79 return isinstance(other, self.__class__) and self.__term == other.__term
80
81 def __str__(self):
82 return "action <%s>" % ('' if not self.__term else str(self.__term))
32 83
33 class AutomatonState(object): 84 class AutomatonState(object):
34 '''A base class for dfa and nfa states. Immutable''' 85 '''A base class for dfa and nfa states. Immutable'''
35 86
36 __node_number_counter = 0 87 __node_number_counter = 0
37 88
38 def __init__(self): 89 def __init__(self):
39 self.__node_number = AutomatonState.__node_number_counter 90 self.__node_number = AutomatonState.__node_number_counter
40 AutomatonState.__node_number_counter += 1 91 AutomatonState.__node_number_counter += 1
41 92
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 last_position = pos 201 last_position = pos
151 # lex next token 202 # lex next token
152 valid_states = Automaton.__transition_states_for_char(self.start_set(), c) 203 valid_states = Automaton.__transition_states_for_char(self.start_set(), c)
153 assert valid_states 204 assert valid_states
154 valid_states = self.__omega_closure(self.epsilon_closure(valid_states)) 205 valid_states = self.__omega_closure(self.epsilon_closure(valid_states))
155 action = self.dominant_action(valid_states) 206 action = self.dominant_action(valid_states)
156 if not action: 207 if not action:
157 assert default_action 208 assert default_action
158 action = default_action 209 action = default_action
159 yield (action, last_position, len(string)) 210 yield (action, last_position, len(string))
OLDNEW
« 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