Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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
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]';
''')
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()
"""
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+\]/;
''')
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.
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.
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'))``
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()