mirror of
https://github.com/AdaCore/langkit.git
synced 2026-02-12 12:28:12 -08:00
55 lines
1.5 KiB
Python
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
|