Exemplo n.º 1
0
Arquivo: ast.py Projeto: kived/kvlang
def parsestring(kvstring, logger=None):
	start_time = time()
	cStream = antlr3.ANTLRStringStream(kvstring)
	lexer = kvTokenLexer(cStream)
	tokenstream = antlr3.CommonTokenStream(lexer)
	for token in tokenstream.getTokens():
		printtoken(token)
	parser = kvParser(tokenstream)

	Node.set_source(tokenstream.getTokens(), kvstring)

	result = parser.kvfile()
	finish_time = time()
	if logger:
		logger('debug', 'kvlang: parsed kv file in %0.4fs' % (finish_time - start_time))
	return tokenstream, result, parser.root_node
Exemplo n.º 2
0
def parsefile(filename):
	cStream = antlr3.ANTLRFileStream(filename)
	lexer = kvLexer(cStream)
	
	if debug:
		print 'Lexer'
		print '-----'
		for token in lexer:
			printtoken(token)
		
		print
		
		lexer.reset()
	
	tokens = antlr3.CommonTokenStream(lexer)
	
	if debug:
		print 'Initial Token Stream'
		print '--------------------'
		token_list = tokens.getTokens()
		for token in token_list:
			printtoken(token)
		
		print
	
	tokens.discardOffChannelTokens = True
	indentedSource = kvTokenSource(tokens)
	tokens = antlr3.CommonTokenStream(indentedSource)
	
	if debug:
		print 'Token Stream'
		print '------------'
		for token in tokens.getTokens():
			printtoken(token)
		
		print
	
	Node.set_source(tokens.getTokens(), file(filename).read())
	
	parser = kvParser(tokens)
	result = parser.kvfile()
	return result