Пример #1
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(main_rule=Or(
         Row('example') ^ BarNode,
         Row('example') ^ BazNode,
     ))
     return foo_grammar
Пример #2
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Row('example', foo_grammar.list_rule) ^ BarNode,
         list_rule=Row(
             List(Tok(Token.Number, keep=True) ^ NumberNode)
         ) ^ ListNode,
     )
     return foo_grammar
Пример #3
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=foo_grammar.list_rule,
         list_rule=Row(
             '(', List(foo_grammar.list_item, sep=',', cls=LiteralList),
             ')')[0],
         list_item=Row(Tok(Token.Number, keep=True)) ^ Literal,
     )
     return foo_grammar
Пример #4
0
def lang_def():
    foo_grammar = Grammar('stmts_rule')
    foo_grammar.add_rules(
        def_rule=Row(Tok(Token.Identifier, keep=True),
                     Opt(Row('(', foo_grammar.stmts_rule, ')')[1])) ^ Def,
        stmt_rule=(foo_grammar.def_rule
                   | Row('{', List(foo_grammar.stmt_rule, empty_valid=True),
                         '}') ^ Block),
        stmts_rule=List(foo_grammar.stmt_rule))
    return foo_grammar
Пример #5
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Row('example',
                       Or(foo_grammar.expression,
                          Row('null') ^ NullNode)) ^ ExampleNode,
         number=Tok(Token.Number, keep=True) ^ Number,
         expression=Or(
             Row(foo_grammar.number, ',', foo_grammar.expression)
             ^ Compound, foo_grammar.number),
     )
     return foo_grammar
Пример #6
0
def lang_def():
    @abstract
    @root_grammar_class()
    class FooNode(ASTNode):
        pass

    class ExampleNode(FooNode):
        pass

    foo_grammar = Grammar('main_rule')
    foo_grammar.add_rules(sec_rule=Row('example'), )

    foo_grammar.add_rules(sec_rule=Row('example'),
                          main_rule=Row(foo_grammar.sec_rule) ^ ExampleNode)
Пример #7
0
    def lang_def():
        @root_grammar_class()
        class FooNode(ASTNode):
            b = Property(lit)

        foo_grammar = Grammar('main_rule')
        foo_grammar.add_rules(main_rule=Row('example') ^ FooNode)
        return foo_grammar
Пример #8
0
def lang_def():
    foo_grammar = Grammar('item')
    foo_grammar.add_rules(
        item=Or(foo_grammar.example, foo_grammar.example_list),
        example=Row('example') ^ Example,
        example_list=ExampleList(
            '(', List(foo_grammar.item), ')'
        )
    )
    return foo_grammar
Пример #9
0
def lang_def():
    @root_grammar_class()
    class FooNode(ASTNode):
        pass

    class Def(FooNode):
        name = Field()
        body = Field()
        env_spec = EnvSpec(add_env=True, add_to_env=(Self.name, Self))

    foo_grammar = Grammar('stmt_rule')
    foo_grammar.add_rules(
        def_rule=Row(
            Tok(Token.Identifier, keep=True),
            '(', foo_grammar.stmt_rule, ')'
        ) ^ Def,
        stmt_rule=List(
            foo_grammar.def_rule
            | Row('{', List(foo_grammar.stmt_rule, empty_valid=True), '}')[1],
            empty_valid=True
        )
    )
    return foo_grammar
Пример #10
0
def lang_def():
    @abstract
    @root_grammar_class()
    class FooNode(ASTNode):
        pass

    class ExampleNode(FooNode):
        pass

    class UnreferencedNode(FooNode):
        untyped_field = Field()

    foo_grammar = Grammar('main_rule')
    foo_grammar.add_rules(main_rule=Row('example') ^ ExampleNode)
    return foo_grammar
Пример #11
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Row('example') ^ Example,
     )
     return foo_grammar
Пример #12
0
from langkit.parsers import Grammar, Row, Tok

from lexer_example import Token
from utils import build_and_run

Diagnostics.set_lang_source_dir(os.path.abspath(__file__))


@root_grammar_class()
class FooNode(ASTNode):
    prop = AbstractProperty(runtime_check=True, type=LongType)


class BarNode(FooNode):
    pass


class Literal(FooNode):
    tok = Field()

    a = AbstractProperty(runtime_check=True, type=FooNode.env_el())
    var = UserField(LogicVarType, is_private=True)

    b = Property(Bind(Self.var, Self.a), private=True)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(main_rule=Row(Tok(Token.Number, keep=True)) ^ Literal, )
build_and_run(foo_grammar, 'main.py')
print 'Done'
Пример #13
0
    pass


@abstract
class Expression(FooNode):
    result = AbstractProperty(type=LongType)


class Literal(Expression):
    tok = Field()

    result = ExternalProperty()


class Plus(Expression):
    left = Field()
    right = Field()

    result = Property(Self.left.result + Self.right.result)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Or(
        Row(foo_grammar.atom, '+', foo_grammar.main_rule) ^ Plus,
        foo_grammar.atom),
    atom=Row(Tok(Token.Number, keep=True)) ^ Literal,
)
build_and_run(foo_grammar, 'main.py')
print 'Done'
Пример #14
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Or(Row('example') ^ ExampleNode,
                      Row('null') ^ NullNode))
     return foo_grammar
Пример #15
0
@abstract
class Stmt(FooNode):
    pass


class Def(Stmt):
    id = Field()
    body = Field()

    env_spec = EnvSpec(add_env=True,
                       add_to_env=add_to_env(Self.id.symbol, Self))


class Block(Stmt):
    items = Field()

    env_spec = EnvSpec(add_env=True)


foo_grammar = Grammar('stmts_rule')
foo_grammar.add_rules(
    def_rule=Row(Tok(Token.Identifier, keep=True),
                 Opt(Row('(', foo_grammar.stmts_rule, ')')[1])) ^ Def,
    stmt_rule=(foo_grammar.def_rule
               | Row('{', List(foo_grammar.stmt_rule, empty_valid=True), '}')
               ^ Block),
    stmts_rule=List(foo_grammar.stmt_rule))

build_and_run(foo_grammar, 'script.py', library_fields_all_public=True)
Пример #16
0
def lang_def():
    foo_grammar = Grammar('main_rule')
    foo_grammar.add_rules(main_rule=Row(List(Tok(Token.Number, keep=True)))
                          ^ ListNode, )
    return foo_grammar
Пример #17
0
class FooNode(ASTNode):
    pass


class Literal(FooNode):
    tok = Field()


class SynthNode(FooNode):
    name = Field(type=Token)
    items = Field(type=Literal.list_type())


class LiteralSequence(FooNode):
    name = Field()
    items = Field()

    prop = Property(New(SynthNode, name=Self.name, items=Self.items),
                    memoized=True)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=foo_grammar.list_rule,
    list_rule=Row('(', Tok(LexToken.Identifier, keep=True),
                  List(foo_grammar.list_item, sep=','), ')') ^ LiteralSequence,
    list_item=Row(Tok(LexToken.Number, keep=True)) ^ Literal,
)
build_and_run(foo_grammar, 'main.py')
print 'Done'
Пример #18
0
 def lang_def():
     foo_grammar = Grammar('main_rule')
     foo_grammar.add_rules(
         main_rule=Row(Tok('example', keep=True)) ^ ExampleNode,
     )
     return foo_grammar
Пример #19
0
    result = ExternalProperty()


class Name(Expression):
    tok = Field()

    designated_unit = ExternalProperty(type=AnalysisUnitType)
    result = Property(Self.designated_unit.root.cast(Expression).result)


class Plus(Expression):
    left = Field()
    right = Field()

    result = Property(Self.left.result + Self.right.result)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(
    main_rule=Or(
        Row(foo_grammar.atom, '+', foo_grammar.main_rule) ^ Plus,
        foo_grammar.atom
    ),
    atom=Or(
        Row(Tok(Token.Number, keep=True)) ^ Literal,
        Row(Tok(Token.Identifier, keep=True)) ^ Name,
    ),
)
build_and_run(foo_grammar, 'main.py')
print 'Done'
Пример #20
0
import os.path

from langkit.compiled_types import ASTNode, root_grammar_class
from langkit.diagnostics import Diagnostics
from langkit.envs import EnvSpec
from langkit.expressions import Self
from langkit.parsers import Grammar, Row

from utils import build_and_run

Diagnostics.set_lang_source_dir(os.path.abspath(__file__))


@root_grammar_class()
class FooNode(ASTNode):
    pass


class Example(FooNode):
    env_spec = EnvSpec(initial_env=Self.parent.parent.children_env)


foo_grammar = Grammar('main_rule')
foo_grammar.add_rules(main_rule=Row('example') ^ Example, )
build_and_run(foo_grammar, 'script.py')