def compositeExpression(rule): rule | expression | (OP, '(', expressionList, ')') rule.astAttrs = { 'operator': OP, 'expressionList': expressionList, 'expression': expression } compositeExpression.astName = 'CompositeExpression' grammar = Grammar(start=compositeExpression, tokens=[SYMBOL, OP], ignore=[WHITE, NEWLINE], ast_tokens=[STRING]) filterParser = Translator(grammar) ast = grammar.ast_classes opTable = { 'and': And, 'or': Or, 'not': Not, 'eq': eq, 'ne': ne, 'lt': lt, 'gt': gt, 'ge': ge, 'le': le, 'wcard': wcard }
#!/usr/bin/env python from css.grammar import grammar ast = grammar.ast_classes from css.tokens import * from codetalker.pgm import Translator t = Translator(grammar) class StyleSheet: pass class Ruleset: def __init__(self, selectors, rules): self.selectors = selectors self.rules = rules @t.translates(ast.StyleSheet) def stylesheet(node): ss = StyleSheet() if node.charset is not None: ss.charset = t.translate(node.charset) ss.imports = list((imp.source, imp.media) for imp in node.imports) ss.rules = [] for section in node.sections: if isinstance(section, ast.Ruleset): rule = t.translate(section) if rule is not None: ss.rules.append(rule) return ss
rule | ('{', [commas((STRING, ':', value))], '}') rule.astAttrs = {'keys': [STRING], 'values': [value]} dict_.astName = 'Dict' def list_(rule): rule | ('[', [commas(value)], ']') rule.astAttrs = {'values': [value]} list_.astName = 'List' grammar = Grammar(start=value, tokens=[SYMBOL], ignore=[WHITE, NEWLINE], ast_tokens=[STRING, TFN, NUMBER]) # translator stuff JSON = Translator(grammar) ast = grammar.ast_classes @JSON.translates(ast.Dict) def t_dict(node): return dict((JSON.translate(key), JSON.translate(value))\ for (key, value) in zip(node.keys, node.values)) @JSON.translates(ast.List) def t_list(node): return list(JSON.translate(value) for value in node.values) @JSON.translates(STRING) def t_string(node): return node.value[1:-1].decode('string_escape')
dble_header_body.ast_name = 'DbleHeaderBody' def body(rule): rule | ("{", units, "}") rule.astAttrs = {"units": units} body.ast_name = 'Body' grammar = Grammar(start=toplevel, tokens=[HEADER, WHITE, NEWLINE, ANY], ignore=[WHITE], ast_tokens=[STRING]) Dict = Translator(grammar) ast = grammar.ast_classes @Dict.translates(ast.Toplevel) def t_toplevel(): return Dict.translate(node.units) @Dict.translates(ast.Units) def t_units(node): dic = {} # h1, h2, b = None, None, None for unit in node.units: tu = Dict.translate(unit) typ = tu[0]
rule.astAttrs = {'expressions':[expression]} if_.astName = 'If' def sexp(rule): rule | ('(',ID,star(expression),')') | ('(',star(expression),')') rule.astAttrs = {'function': ID, 'args': [expression]} sexp.astName = 'Sexp' grammar = Grammar(start=program, tokens=[SYMBOL], ignore=[WHITE, NEWLINE], ast_tokens=[ID, STRING, NUMBER, BOOL]) # translation Lithon = Translator(grammar) ast = grammar.ast_classes elements = [] @Lithon.translates(ast.Program) def t_program(node): for exp in node.body: elements.append(Lithon.translate(exp)) @Lithon.translates(ast.Quote) def t_quote(node): return nodes.Quote([Lithon.translate(v) for v in node.values]) @Lithon.translates(ast.Define) def t_define(node): return nodes.Define(node.args[1],node.args[2:],[Lithon.translate(exp) for exp in node.body])
########NEW FILE######## __FILENAME__ = ctranslator #!/usr/bin/env python from codetalker.pgm import Translator, tokens from errors import TranslateError from grammar import grammar as ccssgrammar, declare_args import grammar import operator import values import consts ccssgrammar.load_rule(declare_args) CCSS = Translator(ccssgrammar, vbls=[consts.defaults.copy()], rule_stack=[], indent=4) ast = ccssgrammar.ast_classes def find_variable(name, scope): for vbls in scope.vbls: if name in vbls: return vbls[name] raise TranslateError('Undefined mixin %s' % name) def handle_body(tree, scope): text, after = '', '' for node in tree.body:
#!/usr/bin/env python from css.grammar import grammar ast = grammar.ast_classes from css.tokens import * from codetalker.pgm import Translator t = Translator(grammar) class StyleSheet: pass class Ruleset: def __init__(self, selectors, rules): self.selectors = selectors self.rules = rules @t.translates(ast.StyleSheet) def stylesheet(node): ss = StyleSheet() if node.charset is not None: ss.charset = t.translate(node.charset) ss.imports = list((imp.source, imp.media) for imp in node.imports) ss.rules = [] for section in node.sections: if isinstance(section, ast.Ruleset):