How to use the tatsu.compile function in TatSu

To help you get started, we’ve selected a few TatSu examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nitros12 / A-Compiler / wewcompiler / parser / __init__.py View on Github external
import os

_lang = os.path.join(os.path.dirname(__file__), "lang.ebnf")

with open(_lang) as f:
    language = f.read()

try:
    from wewcompiler.parser.lang import WewParser
    lang = WewParser()
except ImportError:
    import tatsu
    lang = tatsu.compile(language)
github srix / pytamil / pytamil / தமிழ் / புணர்ச்சி.py View on Github external
def load_parser(filename):
    grammar = open(filename).read()

    parser = tatsu.compile(grammar)
    return parser
    # ast = parser.parse(
    #     '3 + 5 + ( 10 - 20 )',
    #     semantics=CalcSemantics()
    # )

    # ast = parser.parse('நிலைமொழி|உடம்படுமெய்(ய்)|இரட்டுதல் + திரிதல்|வருமொழி ,நிலைமொழி|உடம்படுமெய்(வ்) + திரிதல்|வருமொழி' ,\
    #                      trace = True, colorize =True)



    return ast
github robbert-harms / MOT / mot / lib / cl_data_type.py View on Github external
import tatsu

__author__ = 'Robbert Harms'
__date__ = "2015-03-21"
__license__ = "LGPL v3"
__maintainer__ = "Robbert Harms"
__email__ = "robbert.harms@maastrichtuniversity.nl"


_cl_data_type_parser = tatsu.compile('''
    result = [address_space] {pre_type_qualifiers}* data_type [{post_type_qualifiers}*];

    address_space = ['__'] ('local' | 'global' | 'constant' | 'private');
    pre_type_qualifiers = 'const' | 'volatile';
    
    data_type = type_specifier [vector_length] {array_size}* {pointer}*;
    pointer = '*';
    vector_length = /[2348(16]/;
    array_size = /\[\d+\]/;
    
    post_type_qualifiers = 'const' | 'restrict';
    
    type_specifier = ?'(unsigned )?\w[\w]*[a-zA-Z]';
''')
github neogeny / TatSu / examples / calc / calc.py View on Github external
def annotated_parse():
    grammar = open('grammars/calc_annotated.ebnf').read()

    parser = tatsu.compile(grammar)
    ast = parser.parse('3 + 5 * ( 10 - 20 )')

    print()
    print('# ANNOTATED AST')
    pprint(ast, width=20, indent=4)
    print()
github robbert-harms / MOT / mot / lib / cl_function.py View on Github external
"""
        raise NotImplementedError()

    def get_renamed(self, name):
        """Get a copy of the current parameter but then with a new name.

        Args:
            name (str): the new name for this parameter

        Returns:
            cls: a copy of the current type but with a new name
        """
        raise NotImplementedError()


_cl_data_type_parser = tatsu.compile('''
    result = [address_space] {type_qualifiers}* ctype {pointer_star}* {pointer_qualifiers}* name {array_size}*;

    address_space = ['__'] ('local' | 'global' | 'constant' | 'private');
    type_qualifiers = 'const' | 'volatile';

    basic_ctype = ?'(unsigned )?\w[\w]*[a-zA-Z]';
    vector_type_length = '2' | '3' | '4' | '8' | '16';
    ctype = basic_ctype [vector_type_length];
    pointer_star = '*';

    pointer_qualifiers = 'const' | 'restrict';

    name = /[\w\_\-\.]+/;
    array_size = /\[\d+\]/;
''')
github robbert-harms / MOT / mot / lib / utils.py View on Github external
except OSError:
        return list(map(func, iterable))


_tatsu_cl_function = '''
    function = [address_space] data_type function_name arglist body;
    address_space = ['__'] ('local' | 'global' | 'constant' | 'private');
    data_type = /\w+(\s*(\*)?)+/;
    function_name = /\w+/;
    arglist = '(' @+:arg {',' @+:arg}* ')' | '()';
    arg = /[\w \*\[\]]+/;
    body = compound_statement;    
    compound_statement = '{' {[/[^\{\}]*/] [compound_statement]}* '}';
'''

_extract_cl_functions_parser = tatsu.compile('''
    result = {function}+;
''' + _tatsu_cl_function)

_split_cl_function_parser = tatsu.compile('''
    result = function;
''' + _tatsu_cl_function)


def parse_cl_function(cl_code, dependencies=()):
    """Parse the given OpenCL string to a single SimpleCLFunction.

    If the string contains more than one function, we will return only the last, with all the other added as a
    dependency.

    Args:
        cl_code (str): the input string containing one or more functions.
github robbert-harms / MOT / mot / lib / utils.py View on Github external
documentation = '/*' ->'*/';
    kernel = ['__'] 'kernel';
    address_space = ['__'] ('local' | 'global' | 'constant' | 'private');
    data_type = /\w+(\s*(\*)?)+/;
    function_name = /\w+/;
    arglist = '(' @+:arg {',' @+:arg}* ')' | '()';
    arg = /[\w \*\[\]]+/;
    body = compound_statement;
    compound_statement = '{' {[/[^\{\}]*/] [compound_statement]}* '}';
'''

_extract_cl_functions_parser = tatsu.compile('''
    result = {function}+;
''' + _tatsu_cl_function)

_split_cl_function_parser = tatsu.compile('''
    result = function;
''' + _tatsu_cl_function)


def parse_cl_function(cl_code, dependencies=()):
    """Parse the given OpenCL string to a single SimpleCLFunction.

    If the string contains more than one function, we will return only the last, with all the other added as a
    dependency.

    Args:
        cl_code (str): the input string containing one or more functions.
        dependencies (Iterable[CLCodeObject]): The list of CL libraries this function depends on

    Returns:
        mot.lib.cl_function.SimpleCLFunction: the CL function for the last function in the given strings.
github C4ptainCrunch / ics.py / ics / grammar / parse.py View on Github external
import collections
from pathlib import Path
from typing import Dict, List

import tatsu

CRLF = '\r\n'

grammar_path = Path(__file__).parent.joinpath('contentline.ebnf')

with open(grammar_path) as fd:
    GRAMMAR = tatsu.compile(fd.read())


class ParseError(Exception):
    pass


class ContentLine:
    """
    Represents one property line.

    For example:

    ``FOO;BAR=1:YOLO`` is represented by

    ``ContentLine('FOO', {'BAR': ['1']}, 'YOLO'))``
github neogeny / TatSu / examples / calc / calc.py View on Github external
def parse_and_walk_model():
    grammar = open('grammars/calc_model.ebnf').read()

    parser = tatsu.compile(grammar, asmodel=True)
    model = parser.parse('3 + 5 * ( 10 - 20 )')

    print()
    print('# WALKER RESULT')
    result = CalcWalker().walk(model)
    assert result == -47
    print(result)
    print()