def main(): grammar = regex_parser.translate('(a|b)*') model = grako.genmodel('Regexp', grammar) model.parse('aaabbaba', 'S0') try: model.parse('aaaCbbaba', 'S0') raise Exception('Should not have parsed!') except grako.FailedParse: pass print('Grammar:', file=sys.stderr) print(grammar) sys.stdout.flush() with open(PARSER_FILENAME, 'w') as f: f.write(model.render()) print('Generated parser saved as:', PARSER_FILENAME, file=sys.stderr) print(file=sys.stderr)
from django.core.exceptions import ValidationError from grako.exceptions import FailedParse, SemanticError from functools import reduce import math import operator import inspect import grako import pkg_resources import re from .normalization import get_normalizers grammar_ebnf = pkg_resources.resource_string(__name__, "formula.ebnf") model = grako.genmodel("formula", grammar_ebnf.decode("utf-8")) def get_parser(): return model def validate_variables(variables): """ Check the structure of variables mapping dict and drop extra values. """ clean_variables = {} for key, values in variables.items(): if not re.match('__[0-9]+__', key): raise ValidationError({ 'variables': 'Invalid variable name {}'.format(key) }) valid_defintion = 'type' in values \
import os import json import grako import six import dateutil.parser from grako.exceptions import GrakoException from babbage.exc import QueryException from babbage.util import SCHEMA_PATH with open(os.path.join(SCHEMA_PATH, 'parser.ebnf'), 'rb') as fh: grammar = fh.read().decode('utf8') model = grako.genmodel("all", grammar) class Parser(object): """ Type casting for the basic primitives of the parser, e.g. strings, ints and dates. """ def __init__(self, cube): self.results = [] self.cube = cube self.bindings = [] def string_value(self, ast): text = ast[0] if text.startswith('"') and text.endswith('"'): return json.loads(text) return text
from functools import reduce import inspect import grako import operator import pkg_resources import re from django.core.exceptions import ValidationError from grako.exceptions import FailedParse, SemanticError from .normalization import get_normalizers grammar_ebnf = pkg_resources.resource_string(__name__, "formula.ebnf") model = grako.genmodel("formula", grammar_ebnf.decode("utf-8")) def get_parser(): return model def validate_variables(variables): """ Check the structure of variables mapping dict and drop extra values. """ clean_variables = {} for key, values in variables.items(): if not re.match('__[0-9]+__', key): raise ValidationError( {'variables': 'Invalid variable name {}'.format(key)})
import json grammar = """ file = { block }*; block = id '=' brackets { /\n/ }*; brackets = '{' pos rot hei '}' ; fnum = /-?[0-9]+[.][0-9]+/ ; numarray = { fnum }+ ; pos = 'position=' '{' numarray '}' ; rot = 'rotation=' '{' numarray '}' ; hei = 'height=' '{' numarray '}' ; id = /[0-9]+/ ; """ #comment = /#.*?$/ ; model = grako.genmodel('Bracketed', grammar) #model = grako.genmodel('Bracketed', com) text = """# Endring 1= { position= { 2092.000 2042.000 2082.000 2039.000 2092.000 2042.000 2091.000 2041.000 2096.000 2057.000 } rotation= { 0.000 0.000 0.000 0.000 2.356 } height= { 0.000 0.000 0.000 20.000 0.000 } }
import os import json import grako import six import dateutil.parser from grako.exceptions import GrakoException from babbage.exc import QueryException from babbage.util import SCHEMA_PATH with open(os.path.join(SCHEMA_PATH, 'parser.ebnf'), 'rb') as fh: grammar = fh.read().decode('utf8') model = grako.genmodel("all", grammar) class Parser(object): """ Type casting for the basic primitives of the parser, e.g. strings, ints and dates. """ def __init__(self, cube): self.results = [] self.cube = cube self.bindings = [] def string_value(self, ast): text = ast[0] if text.startswith('"') and text.endswith('"'): return json.loads(text) return text def string_set(self, ast):
""" teststr2 = """\ Action ::= ENUMERATED { -- type description scan (1), -- f1 description locate (2), -- f2 description query (3) -- f3 description } """ teststr3 = """\ Target ::= RECORD { -- type description type TargetType, -- field description specifiers cybox:CyboxObject.&type OPTIONAL -- here's another } """ teststr4 = """\ Duration ::= UTF8String(PATTERN "^PT(\d+H(\d+M(\d+S)?)?|\d+M(\d+S)?|\d+S)$") DateTime ::= UTF8String(PATTERN "^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d{1,6})?(Z|[-+]\d\d:\d\d)$") WhereValue ::= ENUMERATED { internal (1), perimeter (2) } """ pasng = grako.genmodel("xyz", grammar) ast = pasng.parse(teststr4) print(json.dumps(ast, indent=2))