def getGrammar(filename): from dparser import Parser gramgram = open( os.path.join(os.path.dirname(__file__), 'grammargrammar.txt')).read() d_parse_grammar.__doc__ = gramgram parser = Parser() return parser.parse(open(filename).read()).getStructure()
def getGrammar(filename): from dparser import Parser gramgram = open(os.path.join(os.path.dirname(__file__), 'grammargrammar.txt')).read() d_parse_grammar.__doc__ = gramgram parser = Parser() return parser.parse(open(filename).read()).getStructure()
def parse(s, parser=None): if parser is None: parser = Parser(file_prefix='.d_parser_mach_gen') return parser.parse(preprocessor.preprocess(s)).structure
def d_S(t): '''S : d '+' d''' return t[0] + t[2] def d_number(t): '''d : "[0-9]+" ''' return int(t[0]) def skip_space(loc): while loc.s < len(loc.buf) and loc.buf[loc.s:loc.s + len('hello')] == 'hello': loc.s = loc.s + len('hello') parser = Parser(make_grammar_file=1) buf = 'hi10hello+3hellohi' if parser.parse( buf, buf_offset=2, partial_parses=1, initial_skip_space_fn=skip_space).getStructure() != 13: print 'fail' buf = '87+5' if parser.parse(buf, initial_skip_space_fn=skip_space).getStructure() != 92: print 'fail'
# and leaving whitespace intact. from dparser import Parser # turn a tree of strings into a single string (slowly): def stringify(s): if type(s) == str: return s out = '' for c in s: out += stringify(c) return out def d_add1(t, s): "add : add '%' exp" s[1] = '+ ' # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parser.parse('1 % 2 % 3') if stringify(parser.s) != '1 + 2 + 3': print 'error'
global indent_level, start_column if loc.s < len(loc.buf): print('WHITESPACE', loc.s, len(loc.buf), repr(chr(loc.buf[loc.s]))) else: print('WHITESPACE', loc.s, len(loc.buf)) while loc.s < len(loc.buf): c = chr(loc.buf[loc.s]) if c == '\n': start_column = 0 elif c.isspace(): start_column += 1 #TODO Remove # to end of line else: # c is not whitespace, so start_column is new indent level return loc.s += 1 p = Parser() print(dir(p)) p.inserted_thing = 'YAH' #print(p.parse('2+3+4', initial_skip_space_fn=whitespace).getStructure()) #print() s = open('numbo3.farg', 'r').read() x = p.parse(s, initial_skip_space_fn=whitespace) print(x.getStructure()) generate_code(x.getStructure()) #print() #y = p.parse(" blah\nx\n", initial_skip_space_fn=whitespace)
# turn a tree of strings into a single string (slowly): def stringify(s): if type(s) == str: return s out = '' for c in s: out += stringify(c) return out def d_add1(t, s): "add : add '%' exp" s[1] = '+ ' # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parsedmessage = parser.parse('1 % 2 % 3') if stringify(parsedmessage.getStringLeft()) != '1 + 2 + 3': print 'error'
from dparser import Parser # turn a tree of strings into a single string (slowly): def stringify(s): if not isinstance(s, str): return "".join(map(stringify, s)) return s def d_add1(t, s): "add : add '%' exp" s[1] = "+ " # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parsedmessage = parser.parse("1 % 2 % 3") if stringify(parsedmessage.getStringLeft()) != "1 + 2 + 3": print "error"
def parse(s): parser = Parser() return parser.parse(s, print_debug_info=0).getStructure()
from dparser import Parser def d_s(t): "s : a" def d_a(t, spec): "a ::= 'a'" parser = Parser() parser.parse("a")
from dparser import Parser def d_S(t): '''S : d '+' d''' return t[0] + t[2] def d_number(t): '''d : "[0-9]+" ''' return int(t[0]) def skip_space(loc): while loc.s < len(loc.buf) and loc.buf[loc.s:loc.s+len('hello')] == 'hello': loc.s = loc.s + len('hello') parser = Parser(make_grammar_file=1) buf = 'hi10hello+3hellohi' if parser.parse(buf, buf_offset=2, partial_parses=1, initial_skip_space_fn = skip_space).getStructure() != 13: print 'fail' buf = '87+5' if parser.parse(buf, initial_skip_space_fn = skip_space).getStructure() != 92: print 'fail'
# and leaving whitespace intact. from dparser import Parser # turn a tree of strings into a single string (slowly): def stringify(s): if type(s) == str: return s out = '' for c in s: out += stringify(c) return out def d_add1(t, s): "add : add '%' exp" s[1] = '+ ' # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parsedmessage = parser.parse('1 % 2 % 3') if stringify(parsedmessage.getStringLeft()) != '1 + 2 + 3': print 'error'
""" infix_op: '+' | '-' | '*' | '/' | '.' """ return t[0] def d_string(t): """ string: "'" "[^']*" "'" """ return ConstString(t[1]) def d_variable(t): """ variable: '$' identifier """ return Variable(t[1]) def d_identifier(t): """ identifier: "[a-zA-Z_][a-zA-Z0-9_]*" """ return Identifier(t[0]) parser = Parser() print parser.parse("(print 'abc')").emit()
def parse(program): parser = Parser(file_prefix='.d_parser_assign') # ohne die ambiguity function gibt es einen segfault return Module(parser.parse(program, ambiguity_fn=lambda a: None).structure)