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

Side by Side Diff: tools/lexer_generator/code_generator.py

Issue 78713002: Experimental lexer generator: generate code for utf-16 character classes. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Code review (dcarney) 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/code_generator.jinja ('k') | tools/lexer_generator/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
(...skipping 17 matching lines...) Expand all
28 import os 28 import os
29 import sys 29 import sys
30 import jinja2 30 import jinja2
31 from dfa import Dfa 31 from dfa import Dfa
32 from transition_keys import TransitionKey 32 from transition_keys import TransitionKey
33 33
34 class CodeGenerator: 34 class CodeGenerator:
35 35
36 def __init__(self, 36 def __init__(self,
37 rule_processor, 37 rule_processor,
38 char_type, 38 encoding = 'latin1',
39 minimize_default = True, 39 minimize_default = True,
40 inline = True, 40 inline = True,
41 switching = True, 41 switching = True,
42 debug_print = False, 42 debug_print = False,
43 log = False): 43 log = False):
44 if minimize_default: 44 if minimize_default:
45 dfa = rule_processor.default_automata().minimal_dfa() 45 dfa = rule_processor.default_automata().minimal_dfa()
46 else: 46 else:
47 dfa = rule_processor.default_automata().dfa() 47 dfa = rule_processor.default_automata().dfa()
48 self.__dfa = dfa 48 self.__dfa = dfa
49 self.__default_action = rule_processor.default_action 49 self.__default_action = rule_processor.default_action
50 self.__debug_print = debug_print 50 self.__debug_print = debug_print
51 self.__start_node_number = self.__dfa.start_state().node_number() 51 self.__start_node_number = self.__dfa.start_state().node_number()
52 self.__log = log 52 self.__log = log
53 self.__inline = inline 53 self.__inline = inline
54 self.__switching = switching 54 self.__switching = switching
55 self.__char_type = char_type 55 self.__encoding = encoding
56 56
57 def __state_cmp(self, left, right): 57 def __state_cmp(self, left, right):
58 if left['original_node_number'] == self.__start_node_number: 58 if left['original_node_number'] == self.__start_node_number:
59 return -1 59 return -1
60 if right['original_node_number'] == self.__start_node_number: 60 if right['original_node_number'] == self.__start_node_number:
61 return 1 61 return 1
62 c = cmp(len(left['disjoint_keys']), len(right['disjoint_keys'])) 62 c = cmp(len(left['disjoint_keys']), len(right['disjoint_keys']))
63 if c: 63 if c:
64 return c 64 return c
65 c = cmp(left['disjoint_keys'], right['disjoint_keys']) 65 c = cmp(left['disjoint_keys'], right['disjoint_keys'])
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 default_action = self.__default_action 235 default_action = self.__default_action
236 assert(default_action and default_action.match_action()) 236 assert(default_action and default_action.match_action())
237 default_action = default_action.match_action() 237 default_action = default_action.match_action()
238 238
239 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 239 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
240 template_env = jinja2.Environment( 240 template_env = jinja2.Environment(
241 loader = jinja2.PackageLoader('lexer_generator', '.'), 241 loader = jinja2.PackageLoader('lexer_generator', '.'),
242 undefined = jinja2.StrictUndefined) 242 undefined = jinja2.StrictUndefined)
243 template = template_env.get_template('code_generator.jinja') 243 template = template_env.get_template('code_generator.jinja')
244 244
245 if self.__encoding == 'latin1':
246 char_type = 'uint8_t'
247 elif self.__encoding == 'utf16':
248 char_type = 'uint16_t'
249 else:
250 raise Exception('Unsupported encoding %s' % encoding)
245 return template.render( 251 return template.render(
246 start_node_number = 0, 252 start_node_number = 0,
247 debug_print = self.__debug_print, 253 debug_print = self.__debug_print,
248 default_action = default_action, 254 default_action = default_action,
249 dfa_states = dfa_states, 255 dfa_states = dfa_states,
250 char_type = self.__char_type) 256 encoding = self.__encoding,
257 char_type = char_type)
OLDNEW
« no previous file with comments | « tools/lexer_generator/code_generator.jinja ('k') | tools/lexer_generator/generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698