class ApplyParenthesis(ArrangementRule): """ Applies the transformation:: BEGIN_MACRO("⦅") BEGIN ⋅ END END_MACRO ⟨⦅⦆⟩ ⋅ Signals error if there is more than one ``BEGIN END`` sequence. """ def __init__(self): ArrangementRule.__init__(self, "Application Parenthesis") self.block_arrangement = Segment(Form) def applies(self, element): return Util.is_opening_delimiter(element, '⦅') def apply(self, element) -> Element: unique_block = Util.has_unique_block(element) if unique_block: # BEGIN_MACRO BEGIN ... END END_MACRO => BEGIN ... END parent = element.parent next_element = element.next next_element.range.first_position = element.range.first_position next_element.end.range.position_after = element.end.range.position_after parent.remove(element) # remove BEGIN_MACRO '⦅' parent.remove(element.end) # remove END_MACRO '⦆' return self.block_arrangement.apply(next_element) else: raise TokenizingError( element.range, "Unexpected multiple blocks inside double-parenthesis.")
class ApplyParenthesis(ArrangementRule): """ Applies the transformation:: BEGIN_MACRO("⦅") BEGIN ⋅ END END_MACRO ⟨⦅⦆⟩ ⋅ Signals error if there is more than one ``BEGIN END`` sequence. """ def __init__(self): ArrangementRule.__init__(self, "Application Parenthesis") self.block_arrangement = Segment(Form) def applies(self, element): return Util.is_opening_delimiter(element, '⦅') def apply(self, element) -> Element: unique_block = Util.has_unique_block(element) if unique_block: # BEGIN_MACRO BEGIN ... END END_MACRO => BEGIN ... END parent = element.parent next_element = element.next next_element.range.first_position = element.range.first_position next_element.end.range.position_after = element.end.range.position_after parent.remove(element) # remove BEGIN_MACRO '⦅' parent.remove(element.end) # remove END_MACRO '⦆' return self.block_arrangement.apply(next_element) else: raise TokenizingError(element.range, "Unexpected multiple blocks inside double-parenthesis.")
def __init__(self): ArrangementRule.__init__(self, "Application Parenthesis") self.block_arrangement = Segment(Form)
def define_default_anoky_transducer_chain(): global default_python_transducer_chain tt_constituent = TopDownTreeTransducer("Constituent", Arrangement([Constituent(int, float)])) # comments, \, indentation, strings tt_primary = TopDownTreeTransducer("Primary", Arrangement([ #Comment(), RawComment(), AssignmentSegment(), Segment(), ParenthesisWithHead(), ParenthesisNoHead(), LispMode(), #BracketsWithHead({'['}), Delimiters({'[','{'}), CodeQuote({'`','```'}), Strings({"'",'"', "'''", '"""'}, str), # "'''",'"""'}), LeftRightBinaryTokenCapturingOperator({'.'}), ])) tt_infix_special_ops = TopDownTreeTransducer("Infix Gather-Alls", Arrangement([ MultipleAssignment(), ApplyInIsolation({'del', 'pass', 'break', 'continue', 'import', 'global', 'nonlocal', 'assert'}), ApplyToRest({ 'return', 'raise', 'yield',}),])) tt_punctuation = TopDownTreeTransducer("Punctuation", Arrangement([Skip2Punctuation({'@[]', '@{}'}), ForPunctuation(), DefaultSeqPunctuation(), DefaultFormPunctuation()])) # tt_commaoperator = TopDownTreeTransducer("Comma as binary operator", # Arrangement([RightLeftBinaryOperator({','})])) tt_unary_minus = TopDownTreeTransducer("Additive inverse", Arrangement([ LeftRightUnaryPrefixNospaceOperator({'-'})])) tt_exponentiation = TopDownTreeTransducer("Exponentiation", Arrangement([ LeftRightBinaryOperator({'**'})])) tt_tilde = TopDownTreeTransducer("Bitwise NOT", Arrangement([LeftRightUnaryPrefixNospaceOperator({'~'})])) tt_multiplication = TopDownTreeTransducer("Multiplication", Arrangement([ LeftRightBinaryOperator({ '*', '@', '/', '%'})])) tt_addition = TopDownTreeTransducer("Addition", Arrangement([ LeftRightBinaryOperator({'+', '-'})])) tt_bit_shift = TopDownTreeTransducer("Bit Shifts", Arrangement([LeftRightBinaryOperator({'<<', '>>'})])) tt_bit_and = TopDownTreeTransducer("Bitwise AND", Arrangement([LeftRightBinaryOperator({'&'})])) tt_bit_xor = TopDownTreeTransducer("Bitwise XOR", Arrangement([LeftRightBinaryOperator({'^'})])) tt_bit_or = TopDownTreeTransducer("Bitwise OR", Arrangement([LeftRightBinaryOperator({'|'})])) # tt_join_isnt_notin = TopDownTreeTransducer("Comparisons, Membership/Identity Tests", # Arrangement([ # ])) tt_comparisons = TopDownTreeTransducer("Compare", Arrangement([ LeftRightNaryOperatorMultipleHeads('compare', {'==', '!=', '<', '>', '<=', '>=', 'in', 'notin', 'is', 'isnot'}), ])) tt_not = TopDownTreeTransducer("Not", Arrangement([RightLeftUnaryPrefixOperator({'not'})])) tt_and = TopDownTreeTransducer("And", Arrangement([LeftRightNaryOperator({'and'})])) tt_or = TopDownTreeTransducer("Or", Arrangement([LeftRightNaryOperator({'or'})])) tt_conditional = BottomUpTreeTransducer("Conditionals", Arrangement([ InfixIfElse({('if', 'else')}), FormWithDirectives('if', {'elif', 'else'}), FormWithDirectives('try', {'except', 'else', 'finally'})])) #tt_lambda = TopDownTreeTransducer("Lambdas", Arrangement([ ])) tt_assignment = TopDownTreeTransducer("Assignments", Arrangement([ RightLeftBinaryOperator({'=', '+=', '-=', '*=', '/=', '//=', '%=', '@=', '<<=', '>>=', '&=', '^=', '|=' })])) # add to transducer chain tt_starguments = TopDownTreeTransducer("Prefixed Stars/Doublestars", Arrangement([ LeftRightUnaryPrefixNospaceOperator({'*', '**'}) ])) tt_as = TopDownTreeTransducer("infix as", Arrangement([ LeftRightBinaryOperator({'as'})])) default_python_transducer_chain = [ tt_constituent, tt_primary, tt_infix_special_ops, tt_punctuation, tt_unary_minus, tt_exponentiation, tt_tilde, tt_multiplication, tt_addition, tt_starguments, tt_bit_shift, tt_bit_and, tt_bit_xor, tt_bit_or, tt_as, tt_comparisons, tt_not, tt_and, tt_or, tt_conditional, #tt_lambda, tt_assignment, ConvertPreforms() ]
def define_default_lyc_transducer_chain(): global default_lyc_transducer_chain #region transducers tt_constituent = \ TopDownTreeTransducer("Constituent", Arrangement([Constituent()])) tt_primary = TopDownTreeTransducer( "Primary", Arrangement([ Comment(), RawComment(), LeftRightUnaryPrefixNospaceTokenCapturingOperator( {'\\', '~', '$', '', ''}), # etc LeftRightBinaryTokenCapturingOperator({'.', '/'}), LeftRightUnaryPostfixNospaceOperator( {'+o', '', '', '', '*', '', '', ''}), Segment(), ParenthesisWithHead(), #head( args ) => (head args) ParenthesisNoHead(), ApplyParenthesis(), # ArgSeqArrangement(), Delimiters({'[', '⟦', '{', '⦃'}), #Quote('‘'), #Strings(), ])) # tt_block = TopDownTreeTransducer("Block", Arrangement([Block()])) tt_punctuation = TopDownTreeTransducer("Punctuation", Arrangement([DefaultPunctuation()])) tt_infix_special_ops = TopDownTreeTransducer( "Infix Gather-Alls", Arrangement([ #TRF_AssignmentStyleOperator(assignment_symbols), # TFR_MarkSpecialOperator(':=', False), TransformationArrow({'<-', '', '', '', ''}), ApplyToRest({'return'}), ])) tt_prefix = TopDownTreeTransducer( "Prefix Operators", Arrangement( [ RightLeftUnaryPrefixNospaceOperator( {"'", '', '!', '', '*', '&', '\\@', '~@'}), # TFR_RightLeftUnaryPrefixOperator({'make'}) ], ReadDirection.RIGHT_TO_LEFT)) tt_product = TopDownTreeTransducer( "Product and Division", Arrangement([LeftRightNaryOperator({'', '', '%'})])) tt_addition = TopDownTreeTransducer( "Addition and Subtraction", Arrangement([LeftRightNaryOperator({'', ''})])) tt_shift = TopDownTreeTransducer( "Left and Right Shift", Arrangement([LeftRightNaryOperator({'<<', '>>'})])) # # tt_dots = TopDownTreeTransducer("Two-dots", # Arrangement([ # LeftRightBinaryNospaceOperator({'‥'}), # LeftRightUnaryPostfixOperator({'‥'}), # ])) tt_casting = TopDownTreeTransducer( "Casting", Arrangement([LeftRightBinaryOperator({'as', 'to', 'of', 'cast-as'})])) tt_arrow = TopDownTreeTransducer( "Arrows", Arrangement([RightLeftBinaryOperator({'->', '', '', ''})], ReadDirection.RIGHT_TO_LEFT)) tt_comparison = TopDownTreeTransducer( "Comparisons", Arrangement([ LeftRightNaryOperator({'<', '>', '≤', '≥'}), LeftRightBinaryOperatorTwoSymbols({('not', 'in')}), LeftRightBinaryOperator({'in', '∈', '∉'}), ])) tt_equality = TopDownTreeTransducer( "Equality", Arrangement([LeftRightNaryOperator({'=', '≠'})])) tt_bit_and = TopDownTreeTransducer( "Bitwise AND", Arrangement([LeftRightNaryOperator({'&'})])) tt_bit_xor = TopDownTreeTransducer( "Bitwise XOR", Arrangement([LeftRightNaryOperator({'^'})])) tt_bit_or = TopDownTreeTransducer( "Bitwise OR", Arrangement([LeftRightNaryOperator({'|'})])) tt_prefix_not = TopDownTreeTransducer( "Bitwise OR", Arrangement([RightLeftUnaryPrefixOperator({'not'})], ReadDirection.RIGHT_TO_LEFT)) tt_bool_and = TopDownTreeTransducer( "Boolean AND", Arrangement([LeftRightNaryOperator({'and'})])) tt_bool_or = TopDownTreeTransducer( "Boolean OR", Arrangement([LeftRightNaryOperator({'or'})])) tt_if_else = TopDownTreeTransducer( "If-Elif-Else and Infix If-Else", Arrangement([InfixIfElse({('if', 'else')}), FormWithDirectives()])) tt_infix_for = TopDownTreeTransducer( "Infix For", Arrangement([LeftRightBinaryOperatorReversedArgs({'for'})])) _assignment_symbols = { ':=', '+=', '−=', '×=', '÷=', '%=', '>>=', '<<=', '&=', '^=', '|=', '≤_t', 'and=', 'or=' } tt_assignments = TopDownTreeTransducer( "Assignments", Arrangement([RightLeftBinaryOperator(_assignment_symbols)], ReadDirection.RIGHT_TO_LEFT)) tt_meta_and = TopDownTreeTransducer( "Meta AND", Arrangement([ LeftRightNaryOperator({''}), ])) tt_meta_or = TopDownTreeTransducer( "Meta OR", Arrangement([ LeftRightNaryOperator({''}), ])) #endregion default_lyc_transducer_chain = [ tt_constituent, tt_primary, # tt_block, tt_infix_special_ops, tt_prefix, tt_punctuation, tt_product, tt_addition, tt_shift, # tt_dots, tt_casting, tt_arrow, tt_comparison, tt_equality, tt_bit_and, tt_bit_xor, tt_bit_or, tt_prefix_not, tt_bool_and, tt_bool_or, tt_if_else, tt_infix_for, tt_assignments, tt_meta_and, tt_meta_or, ConvertPreforms() ]