def simple_selector(rule): postops = hash, class_, attrib, pseudo rule | (_or(NODE_NAME, '*'), star(_or(postops))) rule | plus(_or(postops)) rule.dont_ignore = True rule.astAttrs = { 'node': NODE_NAME, 'post': postops, }
def rule_def(rule): rule | (CSSSELECTOR, plus(NEWLINE), INDENT, plus(_or(statement, attribute, NEWLINE)), _or(DEDENT, EOF)) rule.astAttrs = { 'selector': { 'type': CSSSELECTOR, 'single': True }, 'body': [statement, attribute] }
def attrib(rule): rule | ('[', cssid, _or('=', no_ignore('|', '='), no_ignore( '~', '=')), _or(cssid, STRING, SSTRING), ']') rule.astAttrs = { 'name': cssid, 'op': [SYMBOL], 'value': { 'type': [cssid, STRING, SSTRING], 'single': True, 'start': 1, } }
def attrib(rule): rule | ('[', cssid, _or('=', no_ignore('|','='), no_ignore('~','=')), _or(cssid, STRING, SSTRING), ']') rule.astAttrs = { 'name':cssid, 'op':[SYMBOL], 'value':{ 'type':[cssid, STRING, SSTRING], 'single':True, 'start':1, } }
def declaration(rule): rule | (cssid, ':', plus(value), [important], ';') rule | (plus(_not(_or(';', '}'))), ';') rule.astAttrs = { 'property': cssid, 'values': [value], 'important': important, }
def unit(rule): rule | _or(dble_header_body, header_header, header_body, single_header) rule.astAttrs = { "t1": header_header, "t2": header_body, "t3": dble_header_body, "t4": single_header }
def declaration(rule): rule | (cssid, ':', plus(value), [important], ';') rule | (plus(_not(_or(';', '}'))), ';') rule.astAttrs = { 'property':cssid, 'values':[value], 'important':important, }
def declare(rule): rule | ('@', CSSID, '(', [commas(expression)], ')', _or(NEWLINE, EOF)) rule.astAttrs = { 'name': { 'type': CSSID, 'single': True }, 'args': expression }
def atomic(rule): rule | (literal, star(_or(post_attr, post_subs, post_call))) rule.astAttrs = { 'literal': { 'type': literal, 'single': True }, 'posts': (post_attr, post_subs, post_call) }
def attribute(rule): rule | (CSSID, ':', value, _or(NEWLINE, EOF)) rule.astAttrs = { 'attr': { 'type': CSSID, 'single': True }, 'value': { 'type': value, 'single': True } }
def assign(rule): rule | (CSSID, '=', value, _or(NEWLINE, EOF)) rule.astAttrs = { 'left': { 'type': CSSID, 'single': True }, 'value': { 'type': value, 'single': True } }
def rgb(rule): rule | ('rgb', '(', ((_or(percentage, NUMBER), ',')*3)[:-1], ')') rule.astAttrs = { 'red':{'type':[percentage, NUMBER], 'single':True, 'start':0}, 'green':{'type':[percentage, NUMBER], 'single':True, 'start':1}, 'blue':{'type':[percentage, NUMBER], 'single':True, 'start':2}, }
def mul_ex(rule): rule | (atomic, star(_or(*'*/'), atomic)) rule.astAttrs = { 'left': { 'type': atomic, 'single': True }, 'ops': SYMBOL, 'values': { 'type': atomic, 'start': 1 } }
def meta(rule): rule | (next, star(_or(*ops), next)) rule.astAttrs = { 'left': { 'type': next, 'single': True }, 'ops': SYMBOL, 'values': { 'type': next, 'start': 1 } }
def expression(rule): rule | (mul_ex, star(_or(*'-+'), mul_ex)) rule.astAttrs = { 'left': { 'type': mul_ex, 'single': True }, 'ops': SYMBOL, 'values': { 'type': mul_ex, 'start': 1 } }
def compilationUnit(rule): rule | (annotations, _or( ( packageDeclaration, star(importDeclaration), star(typeDeclaration) ) , ( classOrInterfaceDeclaration, star(typeDeclaration) ) ) ) | ( [packageDeclaration], star(importDeclaration), star(typeDeclaration) ) rule.astAttrs = { 'packageDeclaration' : packageDeclaration , 'imports' : [importDeclaration] , 'typeDeclarations' : [classOrInterfaceDeclaration] + [typeDeclaration] }
def statement(rule): rule | block \ | (ASSERT, expression, [':', expression], ';') \ | ('if', parExpression, statement, ['else', statement]) \ | ('for', '(', forControl, ')', statement) \ | ('while', parExpression, statement) \ | ('do', statement, 'while', parExpression, ';') \ | ('try', block, _or( (catches,'finally',block), catches, ('finally', block) ) ) \ | ('switch', parExpression, '{', switchBlockStatementGroups, '}') \ | ('synchronized', parExpression, block) \ | ('return', [expression, ';']) \ | ('throw', expression, ';') \ | ('break', [Identifier], ';') \ | ('continue', [Identifier], ';') \ | ';' \ | (statementExpression, ';') \ | (Identifier, ':', statement)
def start(rule): rule | plus(_or(STRING, ID, NUMBER)) rule.astAttrs = {'values': [STRING, ID, NUMBER]}
def atomic(rule): rule | (literal, star(_or(post_attr, post_subs, post_call))) rule.astAttrs = {'literal':{'type':literal, 'single':True}, 'posts':(post_attr, post_subs, post_call)}
def expression(rule): rule | (mul_ex, star(_or(*'-+'), mul_ex)) rule.astAttrs = {'left': {'type':mul_ex, 'single':True}, 'ops': SYMBOL, 'values': {'type':mul_ex, 'start':1}}
def attribute(rule): rule | (CSSID, ':', value, _or(NEWLINE, EOF)) rule.astAttrs = {'attr': {'type':CSSID, 'single':True}, 'value': {'type':value, 'single':True}}
def rule_def(rule): rule | (CSSSELECTOR, plus(NEWLINE), INDENT, plus(_or(statement, attribute, NEWLINE)), _or(DEDENT, EOF)) rule.astAttrs = {'selector': {'type':CSSSELECTOR, 'single':True}, 'body': [statement, attribute]}
def selector(rule): rule | (simple_selector, star([_or('+', '>')], simple_selector)) rule.astAttrs = { 'parts': [simple_selector, SYMBOL], }
def start(rule): rule | (star(_or(statement, NEWLINE))) rule.astAttrs = {'body': statement}
def primaryExpression(rule): rule | (primary, star(selector), [_or(('+','+'),('-','-'))]) rule.astAttrs = { 'primary' : primary, 'selector' : [selector], 'op' : [SYMBOL] }
def define(rule): rule | (name, _or(':', '='), value) rule.astAttrs = {'name':name, 'value':value}
def creator(rule): rule | (nonWildcardTypeArguments, createdName, classCreatorRest) | (createdName , _or(arrayCreatorRest , classCreatorRest))
def castExpression(rule): rule | ('(', primitiveType, ')', unaryExpression) \ | ('(', _or(_type , expression), ')', unaryExpressionNotPlusMinus) rule.astAttrs = { 'primitive' : primitiveType, 'unary' : unaryExpression, 'type' : _type, 'expr' : expression, 'unarynotplus' : unaryExpressionNotPlusMinus }
def arrayCreatorRest(rule): rule | ('[', _or((']', star('[',']'), arrayInitializer) ,(expression, ']', star('[', expression, ']'), star('[' , ']'))))
def start(rule): rule | star(_or(section, NEWLINE)) rule.astAttrs = {'sections':[section]}
def at(rule): rule | (no_ignore('@', ID), _or(STRING, SSTRING))
def four_sub(rule): rule | (ID, _or('+', '=', '-'), ID, _or(NEWLINE, EOF))
def value(rule): rule | (star(_or(*not_newline)), _or(EOF, (NEWLINE, [INDENT, star(star(_or(*not_newline)), _or(NEWLINE, EOF)), _or(DEDENT, EOF)]))) rule.astAttrs = {'text':list(not_newline)+[NEWLINE]}
def section(rule): rule | (head, star(_or(define, NEWLINE))) rule.astAttrs = {'head':head, 'body':[define]}
def selector(rule): rule | _or(CSSID, SYMBOL) rule.astAttrs = {'items': (CSSID, SYMBOL, WHITE)}
def value(rule): rule | (ID, star('-', _or(INT, ID))) rule.dont_ignore = True
def assign(rule): rule | (CSSID, '=', value, _or(NEWLINE, EOF)) rule.astAttrs = {'left': {'type':CSSID, 'single':True}, 'value': {'type':value, 'single':True}}
def program(rule): rule | (star(_or(expression,NEWLINE)),EOF) rule.astAttrs = {'body': [expression]}
def declare(rule): rule | ('@', CSSID, '(', [commas(expression)], ')', _or(NEWLINE, EOF)) rule.astAttrs = {'name': {'type':CSSID, 'single':True}, 'args': expression}
def declaration_end(rule): rule | (cssid, ':', plus(value), [important], '}') rule | (plus(_not(_or(';', '}'))), '}') rule.astAttrs = { 'property': cssid, 'values': [value], 'important': important, } rule.keep_tree = True declaration_end.astName = 'Declaration' def important(rule): rule | ('!', 'important') rule.dont_ignore = True from values import value, uri block = '{', star(declaration), _or(declaration_end, '}') grammar = Grammar(start=style_sheet, tokens=the_tokens, ignore=[WHITE, CMCOMMENT, NEWLINE], ast_tokens=[COLOR, HEXCOLOR, STRING, SSTRING]) # vim: et sw=4 sts=4
def meta(rule): rule | (next, star(_or(*ops), next)) rule.astAttrs = {'left': {'type':next, 'single':True}, 'ops': SYMBOL, 'values': {'type':next, 'start':1}}
def at(rule): rule | (no_ignore('@', ID), _or(STRING, SSTRING, star(_not(_or(';','}')))), ';') rule | star(_not(_or(';','}')))
def mul_ex(rule): rule | (atomic, star(_or(*'*/'), atomic)) rule.astAttrs = {'left': {'type':atomic, 'single':True}, 'ops': SYMBOL, 'values': {'type':atomic, 'start':1}}
def charset(rule): rule | (no_ignore('@', 'charset'), _or(STRING, SSTRING), ';') rule.pass_single = True
def four_sub(rule): rule | (ID, _or("+", "=", "-"), ID, _or(NEWLINE, EOF))
def cssid(rule): ids = ID, COLOR, NODE_NAME, UNIT rule | plus('-', _or(*ids)) | (_or(*ids), star('-', _or(*ids))) rule.dont_ignore = True rule.astAttrs = {'parts': (SYMBOL, ) + ids}
def unit(rule): rule | _or(dble_header_body , header_header, header_body, single_header) rule.astAttrs = { "t1": header_header, "t2": header_body, "t3": dble_header_body, "t4": single_header }
def name(rule): rule | plus(_or(ID, NUMBER, WHITE)) rule.astAttrs = {'words':[ID, NUMBER, WHITE]}
def value(rule): rule | plus(_or(CSSID, NUMBER, CSSFN)) rule.astAttrs = {'items': (CSSID, NUMBER, CSSFN)}