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

Side by Side Diff: tools/lexer_generator/action.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/gyp/v8.gyp ('k') | tools/lexer_generator/automaton.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2014 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are
4 # met:
5 #
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28 from types import StringType, IntType
29
30 class Term(object):
31 '''An immutable class representing a function and its arguments.
32 f(a,b,c) would be represented as ('f', a, b, c) where
33 a, b, and c are strings, integers or Terms.'''
34
35 __empty_term = None
36
37 @staticmethod
38 def empty_term():
39 if Term.__empty_term == None:
40 Term.__empty_term = Term('')
41 return Term.__empty_term
42
43 def __init__(self, name, *args):
44 assert type(name) == StringType
45 assert name or not args, 'empty term must not have args'
46 for v in args:
47 assert type(v) == IntType or type(v) == StringType or isinstance(v, Term)
48 self.__tuple = tuple([name] + list(args))
49 self.__str = None
50
51 def name(self):
52 return self.__tuple[0]
53
54 def args(self):
55 return self.__tuple[1:]
56
57 def __hash__(self):
58 return hash(self.__tuple)
59
60 def __nonzero__(self):
61 'true <==> self == empty_term'
62 return bool(self.__tuple[0])
63
64 def __eq__(self, other):
65 return (isinstance(other, self.__class__) and self.__tuple == other.__tuple)
66
67 # TODO(dcarney): escape '(', ')' and ',' in strings
68 def __str__(self):
69 if self.__str == None:
70 self.__str = '(%s)' % ','.join(map(str, self.__tuple))
71 return self.__str
72
73 class Action(object):
74
75 __empty_action = None
76
77 @staticmethod
78 def empty_action():
79 if Action.__empty_action == None:
80 Action.__empty_action = Action(Term.empty_term(), -1)
81 return Action.__empty_action
82
83 @staticmethod
84 def dominant_action(actions):
85 dominant = Action.empty_action()
86 for action in actions:
87 if not action:
88 continue
89 if not dominant:
90 dominant = action
91 continue
92 if action.precedence() == dominant.precedence():
93 assert action.__term == dominant.__term
94 elif action.precedence() < dominant.precedence():
95 dominant = action
96 return dominant
97
98 def __init__(self, term, precedence):
99 assert isinstance(term, Term)
100 assert type(precedence) == IntType
101 assert not term or precedence >= 0, 'action must have positive precedence'
102 self.__term = term
103 self.__precedence = precedence
104
105 def name(self):
106 return self.__term.name()
107
108 def term(self):
109 return self.__term
110
111 def precedence(self):
112 return self.__precedence
113
114 def __nonzero__(self):
115 'true <==> self == empty_action'
116 return bool(self.__term)
117
118 def __eq__(self, other):
119 return isinstance(other, self.__class__) and self.__term == other.__term
120
121 def __str__(self):
122 return "action <%s>" % ('' if not self.__term else str(self.__term))
OLDNEW
« no previous file with comments | « tools/gyp/v8.gyp ('k') | tools/lexer_generator/automaton.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698