Files
langkit/testsuite/python_support/utils.py
Pierre-Marie de Rodat e330d024c3 Add a test for property privacy checks
Change-Id: Ic6b3284a11b4266a47ed87972aa8c2b862136706
TN: P404-013
2016-04-06 12:23:26 +02:00

55 lines
1.5 KiB
Python

import os
import shutil
from langkit.compile_context import CompileCtx
from langkit.compiled_types import ASTNode, StructMetaClass, root_grammar_class
from langkit.diagnostics import DiagnosticError
from langkit.expressions import Self
from lexer_example import foo_lexer
def emit_and_print_errors(grammar, main_rule_name='main_rule',
lexer=foo_lexer):
"""
Return wether code emission was successful.
:rtype: bool
"""
# Have a clean build directory
if os.path.exists('build'):
shutil.rmtree('build')
os.mkdir('build')
# Try to emit code
ctx = CompileCtx(lang_name='Foo',
main_rule_name=main_rule_name,
lexer=lexer,
grammar=grammar)
try:
ctx.emit('build', generate_lexer=False)
# ... and tell about how it went
except DiagnosticError:
# If there is a diagnostic error, don't say anything, the diagnostics
# are enough.
return False
else:
print 'Code generation was successful'
return True
def reset_langkit():
"""
Reset global state in Langkit.
TODO: this is a hack to workaround another hack. At some point in the
future, we should get rid of this global state in Langkit.
"""
StructMetaClass.root_grammar_class = None
StructMetaClass.astnode_types = [ASTNode]
StructMetaClass.struct_types = []
StructMetaClass.env_metadata = None
Self.__dict__['_frozen'] = False