<s> from __future__ import unicode_literals <EOL> import sys <EOL> if sys . version < '<STR_LIT:3>' : <EOL> text = unicode <EOL> else : <EOL> text = str <EOL> import re <EOL> from arpeggio import StrMatch , Optional , ZeroOrMore , OneOrMore , Sequence , OrderedChoice , RegExMatch , Match , NoMatch , EOF , ParsingExpression , SemanticAction , ParserPython , SemanticActionSingleChild <EOL> from arpeggio . export import PMDOTExporter <EOL> from arpeggio import RegExMatch as _ <EOL> from . exceptions import TextXSyntaxError , TextXSemanticError <EOL> from . const import MULT_ZEROORMORE , MULT_ONEORMORE , MULT_OPTIONAL , RULE_COMMON , RULE_MATCH , RULE_ABSTRACT <EOL> def textx_model ( ) : return ZeroOrMore ( import_stm ) , ZeroOrMore ( textx_rule ) , EOF <EOL> def import_stm ( ) : return '<STR_LIT>' , grammar_to_import <EOL> def grammar_to_import ( ) : return _ ( r'<STR_LIT>' ) <EOL> def textx_rule ( ) : return rule_name , Optional ( rule_params ) , "<STR_LIT::>" , textx_rule_body , "<STR_LIT:;>" <EOL> def rule_params ( ) : return '<STR_LIT:[>' , rule_param , ZeroOrMore ( '<STR_LIT:U+002C>' , rule_param ) , '<STR_LIT:]>' <EOL> def rule_param ( ) : return param_name , Optional ( '<STR_LIT:=>' , string_value ) <EOL> def param_name ( ) : return ident <EOL> def textx_rule_body ( ) : return sequence <EOL> def sequence ( ) : return OneOrMore ( choice ) <EOL> def choice ( ) : return repeatable_expr , ZeroOrMore ( "<STR_LIT:|>" , repeatable_expr ) <EOL> def repeatable_expr ( ) : return expression , Optional ( repeat_operator ) <EOL> def expression ( ) : return [ assignment , simple_match , rule_ref , bracketed_sequence ] <EOL> def bracketed_sequence ( ) : return '<STR_LIT:(>' , sequence , '<STR_LIT:)>' <EOL> def repeat_operator ( ) : return [ '<STR_LIT:*>' , '<STR_LIT:?>' , '<STR_LIT:+>' ] , Optional ( repeat_modifiers ) <EOL> def repeat_modifiers ( ) : return '<STR_LIT:[>' , OneOrMore ( [ simple_match , <EOL> '<STR_LIT>' ] ) , '<STR_LIT:]>' <EOL> def simple_match ( ) : return [ str_match , re_match ] <EOL> def assignment ( ) : return attribute , assignment_op , assignment_rhs <EOL> def attribute ( ) : return ident <EOL> def assignment_op ( ) : return [ "<STR_LIT:=>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def assignment_rhs ( ) : return [ simple_match , reference ] , Optional ( repeat_modifiers ) <EOL> def reference ( ) : return [ rule_ref , obj_ref ] <EOL> def rule_ref ( ) : return ident <EOL> def obj_ref ( ) : return '<STR_LIT:[>' , class_name , Optional ( '<STR_LIT:|>' , obj_ref_rule ) , '<STR_LIT:]>' <EOL> def rule_name ( ) : return ident <EOL> def obj_ref_rule ( ) : return ident <EOL> def class_name ( ) : return ident <EOL> def str_match ( ) : return [ ( "<STR_LIT:'>" , _ ( r"<STR_LIT>" ) , "<STR_LIT:'>" ) , ( '<STR_LIT:">' , _ ( r'<STR_LIT>' ) , '<STR_LIT:">' ) ] <EOL> def re_match ( ) : return "<STR_LIT:/>" , _ ( r"<STR_LIT>" ) , "<STR_LIT:/>" <EOL> def ident ( ) : return _ ( r'<STR_LIT>' ) <EOL> def integer ( ) : return _ ( r'<STR_LIT>' ) <EOL> def string_value ( ) : return [ _ ( r"<STR_LIT>" ) , <EOL> _ ( r'<STR_LIT>' ) ] <EOL> def comment ( ) : return [ comment_line , comment_block ] <EOL> def comment_line ( ) : return _ ( r'<STR_LIT>' ) <EOL> def comment_block ( ) : return _ ( r'<STR_LIT>' ) <EOL> ID = _ ( r'<STR_LIT>' , rule_name = '<STR_LIT>' , root = True ) <EOL> BOOL = _ ( r'<STR_LIT>' , rule_name = '<STR_LIT>' , root = True ) <EOL> INT = _ ( r'<STR_LIT>' , rule_name = '<STR_LIT>' , root = True ) <EOL> FLOAT = _ ( r'<STR_LIT>' , '<STR_LIT>' , root = True ) <EOL> STRING = _ ( r'<STR_LIT>' , '<STR_LIT>' , root = True ) <EOL> NUMBER = OrderedChoice ( nodes = [ FLOAT , INT ] , rule_name = '<STR_LIT>' , root = True ) <EOL> BASETYPE = OrderedChoice ( nodes = [ NUMBER , BOOL , ID , STRING ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> BASE_TYPE_RULES = { rule . rule_name : rule <EOL> for rule in [ ID , BOOL , INT , FLOAT , <EOL> STRING , NUMBER , BASETYPE ] } <EOL> BASE_TYPE_NAMES = BASE_TYPE_RULES . keys ( ) <EOL> PRIMITIVE_PYTHON_TYPES = [ int , float , text , bool ] <EOL> for regex in [ ID , BOOL , INT , FLOAT , STRING ] : <EOL> regex . compile ( ) <EOL> def python_type ( textx_type_name ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : text , <EOL> '<STR_LIT>' : bool , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : float , <EOL> '<STR_LIT>' : text , <EOL> '<STR_LIT>' : float , <EOL> '<STR_LIT>' : text , <EOL> } . get ( textx_type_name , textx_type_name ) <EOL> class RuleCrossRef ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rule_name , cls , position ) : <EOL> self . rule_name = rule_name <EOL> self . cls = cls <EOL> self . position = position <EOL> def __str__ ( self ) : <EOL> return self . rule_name <EOL> def __unicode__ ( self ) : <EOL> return self . __str__ ( ) <EOL> class ClassCrossRef ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls_name , position = <NUM_LIT:0> ) : <EOL> self . cls_name = cls_name <EOL> self . position = position <EOL> class TextXModelSA ( SemanticAction ) : <EOL> def first_pass ( self , grammar_parser , node , children ) : <EOL> metamodel = grammar_parser . metamodel <EOL> if '<STR_LIT>' in metamodel : <EOL> comments_model = grammar_parser . metamodel [ '<STR_LIT>' ] . _tx_peg_rule <EOL> else : <EOL> comments_model = None <EOL> root_rule = children [ <NUM_LIT:0> ] <EOL> from . model import get_model_parser <EOL> model_parser = get_model_parser ( root_rule , comments_model , <EOL> ignore_case = metamodel . ignore_case , <EOL> skipws = metamodel . skipws , <EOL> ws = metamodel . ws , <EOL> autokwd = metamodel . autokwd , <EOL> debug = metamodel . debug ) <EOL> model_parser . metamodel = metamodel <EOL> return model_parser <EOL> def _resolve_rule_refs ( self , grammar_parser , model_parser ) : <EOL> """<STR_LIT>""" <EOL> resolved_set = set ( ) <EOL> def resolve ( node ) : <EOL> """<STR_LIT>""" <EOL> def _inner_resolve ( rule ) : <EOL> if grammar_parser . debug : <EOL> grammar_parser . dprint ( "<STR_LIT>" . format ( rule ) ) <EOL> initial_rule_name = rule . rule_name <EOL> if type ( rule ) is RuleCrossRef : <EOL> rule_name = rule . rule_name <EOL> if rule_name in model_parser . metamodel : <EOL> rule = model_parser . metamodel [ rule_name ] . _tx_peg_rule <EOL> if type ( rule ) is RuleCrossRef : <EOL> rule = _inner_resolve ( rule ) <EOL> model_parser . metamodel [ rule_name ] . _tx_peg_rule = rule <EOL> else : <EOL> line , col = grammar_parser . pos_to_linecol ( rule . position ) <EOL> raise TextXSemanticError ( <EOL> '<STR_LIT>' <EOL> . format ( rule . rule_name , <EOL> ( line , col ) ) , line , col ) <EOL> assert isinstance ( rule , ParsingExpression ) , "<STR_LIT>" . format ( type ( rule ) , text ( rule ) ) <EOL> if hasattr ( rule , '<STR_LIT>' ) and len ( rule . _tx_class . _tx_attrs ) > <NUM_LIT:0> : <EOL> rule . _tx_class . _tx_type = RULE_COMMON <EOL> abstract = False <EOL> for idx , child in enumerate ( rule . nodes ) : <EOL> if child not in resolved_set : <EOL> resolved_set . add ( rule ) <EOL> child = _inner_resolve ( child ) <EOL> rule . nodes [ idx ] = child <EOL> if rule . rule_name != initial_rule_name : <EOL> abstract = True <EOL> elif rule . root and not rule . rule_name . startswith ( '<STR_LIT>' ) and hasattr ( child , '<STR_LIT>' ) and isinstance ( rule , OrderedChoice ) : <EOL> abstract |= child . _tx_class . _tx_type != RULE_MATCH <EOL> if abstract : <EOL> cls = model_parser . metamodel [ initial_rule_name ] <EOL> cls . _tx_type = RULE_ABSTRACT <EOL> for idx , child in enumerate ( rule . nodes ) : <EOL> if child . root and hasattr ( child , '<STR_LIT>' ) : <EOL> if child . _tx_class not in cls . _tx_inh_by : <EOL> cls . _tx_inh_by . append ( child . _tx_class ) <EOL> return rule <EOL> resolved_set . add ( node ) <EOL> _inner_resolve ( node ) <EOL> if grammar_parser . debug : <EOL> grammar_parser . dprint ( "<STR_LIT>" ) <EOL> for cls in model_parser . metamodel : <EOL> resolve ( cls . _tx_peg_rule ) <EOL> def _resolve_cls_refs ( self , grammar_parser , model_parser ) : <EOL> resolved_classes = { } <EOL> def _resolve_cls ( cls ) : <EOL> if cls in resolved_classes : <EOL> return resolved_classes [ cls ] <EOL> metamodel = model_parser . metamodel <EOL> to_resolve = cls <EOL> if isinstance ( cls , ClassCrossRef ) : <EOL> if cls . cls_name not in metamodel : <EOL> line , col = grammar_parser . pos_to_linecol ( cls . position ) <EOL> raise TextXSemanticError ( <EOL> '<STR_LIT>' <EOL> . format ( cls . cls_name , ( line , col ) ) , line , col ) <EOL> cls = metamodel [ cls . cls_name ] <EOL> resolved_classes [ to_resolve ] = cls <EOL> if cls . _tx_type == RULE_ABSTRACT : <EOL> for idx , inh in enumerate ( cls . _tx_inh_by ) : <EOL> inh = _resolve_cls ( inh ) <EOL> cls . _tx_inh_by [ idx ] = inh <EOL> else : <EOL> for attr in cls . _tx_attrs . values ( ) : <EOL> attr . cls = _resolve_cls ( attr . cls ) <EOL> if attr . cls . __name__ in BASE_TYPE_NAMES or attr . cls . _tx_type == RULE_MATCH : <EOL> attr . ref = False <EOL> attr . cont = True <EOL> else : <EOL> attr . ref = True <EOL> if grammar_parser . debug : <EOL> grammar_parser . dprint ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( cls . __name__ , attr . name , <EOL> attr . cls . __name__ , <EOL> attr . cont , attr . ref , attr . mult , <EOL> attr . position ) ) <EOL> return cls <EOL> if grammar_parser . debug : <EOL> grammar_parser . dprint ( "<STR_LIT>" ) <EOL> for cls in model_parser . metamodel : <EOL> _resolve_cls ( cls ) <EOL> def second_pass ( self , grammar_parser , model_parser ) : <EOL> """<STR_LIT>""" <EOL> if grammar_parser . debug : <EOL> grammar_parser . dprint ( "<STR_LIT>" ) <EOL> self . _resolve_rule_refs ( grammar_parser , model_parser ) <EOL> self . _resolve_cls_refs ( grammar_parser , model_parser ) <EOL> return model_parser <EOL> textx_model . sem = TextXModelSA ( ) <EOL> def import_stm_SA ( parser , node , children ) : <EOL> parser . metamodel . _new_import ( children [ <NUM_LIT:0> ] ) <EOL> import_stm . sem = import_stm_SA <EOL> def grammar_to_import_SA ( parser , node , children ) : <EOL> return text ( node ) <EOL> grammar_to_import . sem = grammar_to_import_SA <EOL> def textx_rule_SA ( parser , node , children ) : <EOL> if len ( children ) > <NUM_LIT:2> : <EOL> rule_name , rule_params , rule = children <EOL> else : <EOL> rule_name , rule = children <EOL> rule_params = { } <EOL> if rule . rule_name . startswith ( '<STR_LIT>' ) or ( isinstance ( rule , Match ) and rule_params ) : <EOL> rule = Sequence ( nodes = [ rule ] , rule_name = rule_name , <EOL> root = True , ** rule_params ) <EOL> else : <EOL> if not isinstance ( rule , RuleCrossRef ) : <EOL> rule . rule_name = rule_name <EOL> rule . root = True <EOL> for param in rule_params : <EOL> setattr ( rule , param , rule_params [ param ] ) <EOL> parser . metamodel . _set_rule ( rule_name , rule ) <EOL> return rule <EOL> textx_rule . sem = textx_rule_SA <EOL> def rule_name_SA ( parser , node , children ) : <EOL> rule_name = str ( node ) <EOL> if parser . debug : <EOL> parser . dprint ( "<STR_LIT>" . format ( rule_name ) ) <EOL> if rule_name in parser . metamodel . user_classes : <EOL> cls = parser . metamodel . user_classes [ rule_name ] <EOL> parser . metamodel . _init_class ( cls , None , node . position ) <EOL> else : <EOL> cls = parser . metamodel . _new_class ( rule_name , None , node . position ) <EOL> parser . _current_cls = cls <EOL> if not parser . metamodel . rootcls : <EOL> parser . metamodel . rootcls = cls <EOL> return rule_name <EOL> rule_name . sem = rule_name_SA <EOL> def rule_params_SA ( parser , node , children ) : <EOL> params = { } <EOL> for name , value in children [ <NUM_LIT:0> ] . items ( ) : <EOL> if name not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise TextXSyntaxError ( <EOL> '<STR_LIT>' <EOL> . format ( name , parser . pos_to_linecol ( node . position ) ) ) <EOL> if name == '<STR_LIT>' and '<STR_LIT:\\>' in value : <EOL> new_value = "<STR_LIT>" <EOL> if "<STR_LIT>" in value : <EOL> new_value += "<STR_LIT:\n>" <EOL> if "<STR_LIT>" in value : <EOL> new_value += "<STR_LIT:\r>" <EOL> if "<STR_LIT>" in value : <EOL> new_value += "<STR_LIT:\t>" <EOL> if "<STR_LIT:U+0020>" in value : <EOL> new_value += "<STR_LIT:U+0020>" <EOL> value = new_value <EOL> params [ name ] = value <EOL> return params <EOL> rule_params . sem = rule_params_SA <EOL> def rule_param_SA ( parser , node , children ) : <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> param_name , param_value = children <EOL> else : <EOL> param_name = children [ <NUM_LIT:0> ] <EOL> param_value = True <EOL> if param_name . startswith ( '<STR_LIT>' ) : <EOL> param_name = param_name [ <NUM_LIT:2> : ] <EOL> param_value = False <EOL> if parser . debug : <EOL> parser . dprint ( "<STR_LIT>" . format ( param_name , <EOL> param_value ) ) <EOL> return { param_name : param_value } <EOL> rule_param . sem = rule_param_SA <EOL> def rule_ref_SA ( parser , node , children ) : <EOL> rule_name = text ( node ) <EOL> return RuleCrossRef ( rule_name , rule_name , node . position ) <EOL> rule_ref . sem = rule_ref_SA <EOL> def textx_rule_body_SA ( parser , node , children ) : <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> return Sequence ( nodes = children [ : ] ) <EOL> else : <EOL> return children [ <NUM_LIT:0> ] <EOL> textx_rule_body . sem = textx_rule_body_SA <EOL> def sequence_SA ( parser , node , children ) : <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> return Sequence ( nodes = children [ : ] ) <EOL> else : <EOL> return children [ <NUM_LIT:0> ] <EOL> sequence . sem = sequence_SA <EOL> def choice_SA ( parser , node , children ) : <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> return OrderedChoice ( nodes = children [ : ] ) <EOL> else : <EOL> return children [ <NUM_LIT:0> ] <EOL> choice . sem = choice_SA <EOL> def repeat_modifiers_SA ( parser , node , children ) : <EOL> modifiers = { } <EOL> for modifier in children : <EOL> if isinstance ( modifier , Match ) : <EOL> modifier . rule_name = '<STR_LIT>' <EOL> modifiers [ '<STR_LIT>' ] = modifier <EOL> elif type ( modifier ) == tuple : <EOL> modifiers [ '<STR_LIT>' ] = modifier <EOL> else : <EOL> modifiers [ '<STR_LIT>' ] = True <EOL> return ( modifiers , node . position ) <EOL> repeat_modifiers . sem = repeat_modifiers_SA <EOL> def repeat_operator_SA ( parser , node , children ) : <EOL> return children <EOL> repeat_operator . sem = repeat_operator_SA <EOL> def repeatable_expr_SA ( parser , node , children ) : <EOL> expr = children [ <NUM_LIT:0> ] <EOL> rule = expr <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> repeat_op = children [ <NUM_LIT:1> ] <EOL> if len ( repeat_op ) > <NUM_LIT:1> : <EOL> repeat_op , modifiers = repeat_op <EOL> else : <EOL> repeat_op = repeat_op [ <NUM_LIT:0> ] <EOL> modifiers = None <EOL> if repeat_op == '<STR_LIT:?>' : <EOL> rule = Optional ( nodes = [ expr ] ) <EOL> elif repeat_op == '<STR_LIT:*>' : <EOL> rule = ZeroOrMore ( nodes = [ expr ] ) <EOL> else : <EOL> rule = OneOrMore ( nodes = [ expr ] ) <EOL> if modifiers : <EOL> modifiers , position = modifiers <EOL> if repeat_op == '<STR_LIT:?>' : <EOL> line , col = parser . pos_to_linecol ( position ) <EOL> raise TextXSyntaxError ( <EOL> '<STR_LIT>' <EOL> . format ( text ( ( line , col ) ) ) , line , col ) <EOL> if '<STR_LIT>' in modifiers : <EOL> sep = modifiers [ '<STR_LIT>' ] <EOL> rule = Sequence ( nodes = [ expr , <EOL> ZeroOrMore ( nodes = [ <EOL> Sequence ( nodes = [ sep , expr ] ) ] ) ] ) <EOL> if repeat_op == '<STR_LIT:*>' : <EOL> rule = Optional ( nodes = [ rule ] ) <EOL> if '<STR_LIT>' in modifiers : <EOL> rule . eolterm = True <EOL> return rule <EOL> repeatable_expr . sem = repeatable_expr_SA <EOL> def assignment_rhs_SA ( parser , node , children ) : <EOL> rule = children [ <NUM_LIT:0> ] <EOL> modifiers = None <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> modifiers = children [ <NUM_LIT:1> ] <EOL> return ( rule , modifiers ) <EOL> assignment_rhs . sem = assignment_rhs_SA <EOL> def assignment_SA ( parser , node , children ) : <EOL> """<STR_LIT>""" <EOL> attr_name = children [ <NUM_LIT:0> ] <EOL> op = children [ <NUM_LIT:1> ] <EOL> rhs_rule , modifiers = children [ <NUM_LIT:2> ] <EOL> cls = parser . _current_cls <EOL> target_cls = None <EOL> if parser . debug : <EOL> parser . dprint ( "<STR_LIT>" . format ( attr_name , op ) ) <EOL> if parser . debug : <EOL> parser . dprint ( "<STR_LIT>" . format ( cls . __name__ , <EOL> attr_name ) ) <EOL> parser . dprint ( "<STR_LIT>" . format ( op ) ) <EOL> if attr_name in cls . _tx_attrs : <EOL> if op == '<STR_LIT>' : <EOL> line , col = parser . pos_to_linecol ( node . position ) <EOL> raise TextXSemanticError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( attr_name , ( line , col ) ) , line , col ) <EOL> cls_attr = cls . _tx_attrs [ attr_name ] <EOL> if cls_attr . mult is not MULT_ONEORMORE : <EOL> cls_attr . mult = MULT_ZEROORMORE <EOL> else : <EOL> cls_attr = parser . metamodel . _new_cls_attr ( cls , name = attr_name , <EOL> position = node . position ) <EOL> if type ( rhs_rule ) is tuple and rhs_rule [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> cls_attr . cont = False <EOL> cls_attr . ref = True <EOL> rhs_rule = rhs_rule [ <NUM_LIT:1> ] <EOL> target_cls = rhs_rule . cls <EOL> base_rule_name = rhs_rule . rule_name <EOL> if op == '<STR_LIT>' : <EOL> assignment_rule = OneOrMore ( <EOL> nodes = [ rhs_rule ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> cls_attr . mult = MULT_ONEORMORE <EOL> elif op == '<STR_LIT>' : <EOL> assignment_rule = ZeroOrMore ( <EOL> nodes = [ rhs_rule ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> if cls_attr . mult is not MULT_ONEORMORE : <EOL> cls_attr . mult = MULT_ZEROORMORE <EOL> elif op == '<STR_LIT>' : <EOL> assignment_rule = Optional ( <EOL> nodes = [ rhs_rule ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> cls_attr . mult = MULT_OPTIONAL <EOL> base_rule_name = '<STR_LIT>' <EOL> cls_attr . bool_assignment = True <EOL> else : <EOL> assignment_rule = Sequence ( <EOL> nodes = [ rhs_rule ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> if modifiers : <EOL> modifiers , position = modifiers <EOL> if op == '<STR_LIT>' or op == '<STR_LIT:=>' : <EOL> line , col = parser . pos_to_linecol ( position ) <EOL> raise TextXSyntaxError ( <EOL> '<STR_LIT>' <EOL> . format ( op , text ( ( line , col ) ) ) , line , col ) <EOL> if '<STR_LIT>' in modifiers : <EOL> sep = modifiers [ '<STR_LIT>' ] <EOL> assignment_rule = Sequence ( <EOL> nodes = [ rhs_rule , <EOL> ZeroOrMore ( nodes = [ Sequence ( nodes = [ sep , rhs_rule ] ) ] ) ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> if op == "<STR_LIT>" : <EOL> assignment_rule . root = False <EOL> assignment_rule = Optional ( nodes = [ assignment_rule ] , <EOL> rule_name = '<STR_LIT>' , root = True ) <EOL> if '<STR_LIT>' in modifiers : <EOL> assignment_rule . eolterm = True <EOL> if target_cls : <EOL> attr_type = target_cls <EOL> else : <EOL> attr_type = base_rule_name if base_rule_name else '<STR_LIT>' <EOL> cls_attr . cls = ClassCrossRef ( cls_name = attr_type , position = node . position ) <EOL> if parser . debug : <EOL> parser . dprint ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( cls . __name__ , attr_name , cls_attr . cls . cls_name , <EOL> cls_attr . cont , cls_attr . ref , cls_attr . mult , <EOL> cls_attr . position ) ) <EOL> assignment_rule . _attr_name = attr_name <EOL> assignment_rule . _exp_str = attr_name <EOL> return assignment_rule <EOL> assignment . sem = assignment_SA <EOL> def str_match_SA ( parser , node , children ) : <EOL> try : <EOL> to_match = children [ <NUM_LIT:0> ] <EOL> except : <EOL> to_match = '<STR_LIT>' <EOL> if parser . metamodel . autokwd : <EOL> match = parser . keyword_regex . match ( to_match ) <EOL> if match and match . span ( ) == ( <NUM_LIT:0> , len ( to_match ) ) : <EOL> regex_match = RegExMatch ( r'<STR_LIT>' . format ( to_match ) , <EOL> ignore_case = parser . metamodel . ignore_case , <EOL> str_repr = to_match ) <EOL> regex_match . compile ( ) <EOL> return regex_match <EOL> return StrMatch ( to_match , ignore_case = parser . metamodel . ignore_case ) <EOL> str_match . sem = str_match_SA <EOL> def re_match_SA ( parser , node , children ) : <EOL> try : <EOL> to_match = children [ <NUM_LIT:0> ] <EOL> except : <EOL> to_match = '<STR_LIT>' <EOL> regex = RegExMatch ( to_match , ignore_case = parser . metamodel . ignore_case ) <EOL> try : <EOL> regex . compile ( ) <EOL> except Exception as e : <EOL> line , col = parser . pos_to_linecol ( node [ <NUM_LIT:1> ] . position ) <EOL> raise TextXSyntaxError ( <EOL> "<STR_LIT>" <EOL> . format ( text ( e ) , text ( ( line , col ) ) ) , line , col ) <EOL> return regex <EOL> re_match . sem = re_match_SA <EOL> def obj_ref_SA ( parser , node , children ) : <EOL> class_name = children [ <NUM_LIT:0> ] <EOL> if class_name in BASE_TYPE_NAMES : <EOL> line , col = parser . pos_to_linecol ( node . position ) <EOL> raise TextXSemanticError ( <EOL> '<STR_LIT>' <EOL> . format ( ( line , col ) ) , line , col ) <EOL> if len ( children ) > <NUM_LIT:1> : <EOL> rule_name = children [ <NUM_LIT:1> ] <EOL> else : <EOL> rule_name = '<STR_LIT>' <EOL> return ( "<STR_LIT>" , RuleCrossRef ( rule_name , class_name , node . position ) ) <EOL> obj_ref . sem = obj_ref_SA <EOL> def integer_SA ( parser , node , children ) : <EOL> return int ( node . value ) <EOL> integer . sem = integer_SA <EOL> def string_value_SA ( parser , node , children ) : <EOL> return node . value . strip ( "<STR_LIT>" ) <EOL> string_value . sem = string_value_SA <EOL> bracketed_sequence . sem = SemanticActionSingleChild ( ) <EOL> expression . sem = SemanticActionSingleChild ( ) <EOL> textX_parsers = { } <EOL> def language_from_str ( language_def , metamodel ) : <EOL> """<STR_LIT>""" <EOL> if metamodel . debug : <EOL> metamodel . dprint ( "<STR_LIT>" ) <EOL> if metamodel . debug in textX_parsers : <EOL> parser = textX_parsers [ metamodel . debug ] <EOL> else : <EOL> parser = ParserPython ( textx_model , comment_def = comment , <EOL> ignore_case = False , <EOL> reduce_tree = False , debug = metamodel . debug ) <EOL> flags = <NUM_LIT:0> <EOL> if metamodel . ignore_case : <EOL> flags = re . IGNORECASE <EOL> parser . keyword_regex = re . compile ( r'<STR_LIT>' , flags ) <EOL> textX_parsers [ metamodel . debug ] = parser <EOL> parser . metamodel = metamodel <EOL> parser . root_rule_name = None <EOL> try : <EOL> parser . parse ( language_def ) <EOL> except NoMatch as e : <EOL> line , col = parser . pos_to_linecol ( e . position ) <EOL> raise TextXSyntaxError ( text ( e ) , line , col ) <EOL> lang_parser = parser . getASG ( ) <EOL> parser . metamodel . validate ( ) <EOL> lang_parser . metamodel = parser . metamodel <EOL> metamodel . parser = lang_parser <EOL> if metamodel . debug : <EOL> PMDOTExporter ( ) . exportFile ( <EOL> lang_parser . parser_model , <EOL> "<STR_LIT>" . format ( metamodel . rootcls . __name__ ) ) <EOL> return lang_parser </s>
