def formatChar0EIi0E(text, pos, context): context['formattedText'] = SyntaxEngine.trimSpaceReturnFromTail(context['formattedText']) \ + '\n' \ + SyntaxEngine.generateIndent(context['indentLevel']) \ + text[pos] \ + '\n' context['isHeadOfLine'] = True pattern = re.compile(r".\s*", re.DOTALL) match = pattern.match(text, pos) return match.end()
def formatChar01(text, pos, context): context['formattedText'] = SyntaxEngine.trimSpaceFromTail(context['formattedText']) \ + text[pos] \ + " " pattern = re.compile(r"[ \t]*") return pattern.match(text, pos+1).end()
def formatHeadOfLine(text, pos, context): context['formattedText'] = context['formattedText'] + \ SyntaxEngine.generateIndent(context['indentLevel']) context['isHeadOfLine'] = False pattern = re.compile(r"[ \t]*") match = pattern.match(text, pos) return match.end()
def formatRightCurlyBrace(text, pos, context): context['indentLevel'] = context['indentLevel'] - 1 pos = formatChar0EIi0E(text, pos, context) pattern = re.compile(r"\s*;") match = pattern.match(text, pos) if match: context['formattedText'] = SyntaxEngine.trimSpaceReturnFromTail(context['formattedText']) \ + ';' \ + '\n' return match.end() return pos
def propertiesFormat(engine): engine.require(matcher=SyntaxEngine.generateRegexMatcher('Properties'), driver=formatAnySymbol) def anonymous1(text, pos, context): context['scope'] = 'properties' context['breakScopeIndentLevel'] = context['indentLevel'] return formatLeftCurlyBrace(text, pos, context) engine.require(matcher=SyntaxEngine.generateRegexMatcher('{'), driver=anonymous1) while engine.context['indentLevel'] > engine.context[ 'breakScopeIndentLevel']: if engine.match('head_of_line'): continue if engine.match('space_char'): continue if engine.match('literal_string'): continue if engine.match('line_comment'): continue if engine.match('block_comment'): continue if engine.match('left_curly_brace'): continue if engine.match('right_curly_brace'): continue if engine.match('return_char'): continue if engine.match('any_punctuation'): continue if engine.match('any_symbol'): continue pass
def format(text): engine = SyntaxEngine(text) engine.add('beginning_of_file', lambda text, pos, context: pos == 0, formatBeginningOfFile) engine.add('head_of_line', lambda text, pos, context: context['isHeadOfLine'] == True, formatHeadOfLine) engine.add('copy_to_eof', lambda text, pos, context: True, copyToEOF) engine.add('literal_string', lambda text, pos, context: text[pos] == r'"', formatLiteralString) engine.add('line_comment', lambda text, pos, context: text[pos:pos + 2] == r'//', formatLineComment) engine.add('block_comment', lambda text, pos, context: text[pos:pos + 2] == r'/*', formatBlockComment) engine.add('char01', matchChar01, formatChar01) engine.add('charP1', matchCharP1, formatCharP1) engine.add('charN0', matchCharN0, formatCharN0) engine.add('left_curly_brace', matchLeftCurlyBrace, formatLeftCurlyBrace) engine.add('right_curly_brace', matchRightCurlyBrace, formatRightCurlyBrace) engine.add('space_char', matchSpaceChar, lambda text, pos, context: BasicFormater.skipSpaces(text, pos)) engine.add('return_char', matchReturnChar, formatReturnChar) engine.add('any_punctuation', matchPunctuation, formatPunctuation) engine.add('any_symbol', matchAnySymbol, formatAnySymbol) engine.add('properties', matchProperties, BasicFormater.formatEmptyString) engine.match('beginning_of_file') mainFormat(engine) return engine.context['formattedText']
if match: context['formattedText'] = context['formattedText'] + match.group() return match.end() r''' CharXY charXXiYYY 0 1 P = Plus >=1 N = Any I = Indent E = Enter. If refactor, rename it to R ''' matchChar01 = SyntaxEngine.generateRegexMatcher(r',') def formatChar01(text, pos, context): context['formattedText'] = BasicFormater.trimSpaceFromTail(context['formattedText']) \ + text[pos] \ + " " pattern = re.compile(r"[ \t]*") return pattern.match(text, pos + 1).end() matchCharP1 = SyntaxEngine.generateRegexMatcher(r'=|:') def formatCharP1(text, pos, context):
def format(text): engine = SyntaxEngine(text) engine.add('beginning_of_file', lambda text, pos, context: pos == 0, formatBeginningOfFile) engine.add('head_of_line', lambda text, pos, context: context['isHeadOfLine'] == True, formatHeadOfLine) engine.add('copy_to_eof', lambda text, pos, context: True, copyToEOF) engine.add('literal_string', lambda text, pos, context: text[pos] == r'"', formatLiteralString) engine.add('line_comment', lambda text, pos, context: text[pos:pos + 2] == r'//', formatLineComment) engine.add('block_comment', lambda text, pos, context: text[pos:pos + 2] == r'/*', formatBlockComment) engine.add('char01', matchChar01, formatChar01) engine.add('charP1', matchCharP1, formatCharP1) engine.add('charN0', matchCharN0, formatCharN0) engine.add('left_curly_brace', matchLeftCurlyBrace, formatLeftCurlyBrace) engine.add('right_curly_brace', matchRightCurlyBrace, formatRightCurlyBrace) engine.add('return_char', matchReturnChar, formatReturnChar) engine.add('any_char', lambda text, pos, context: True, formatAnyChar) engine.match('beginning_of_file') mainFormat(engine) return engine.context['formattedText']
match = pattern.match(text, pos) if match: context['formattedText'] = context['formattedText'] + match.group() return match.end() r''' CharXY charXXiYYY 0 1 P = Plus >=1 N = Any I = Indent E = Enter. If refactor, rename it to R ''' matchChar01 = SyntaxEngine.generateRegexMatcher(r',') def formatChar01(text, pos, context): context['formattedText'] = SyntaxEngine.trimSpaceFromTail(context['formattedText']) \ + text[pos] \ + " " pattern = re.compile(r"[ \t]*") return pattern.match(text, pos+1).end() matchCharP1 = SyntaxEngine.generateRegexMatcher(r'=|:') def formatCharP1(text, pos, context):