def evaluate(self, calculator, divide=False): child = self.child.evaluate(calculator, divide) if isinstance(child, String): contents = child.value quotes = child.quotes else: # TODO compress contents = child.render() quotes = None # TODO unclear if this is the right place for this logic, or if it # should go in the Function constructor, or should be passed in # explicitly by the grammar, or even if Url should go away entirely if self.function_name == "url": return Url(contents, quotes=quotes) else: return Function(contents, self.function_name, quotes=quotes)
def atom(self): _token_ = self._peek(self.u_expr_chks) if _token_ == 'LPAR': LPAR = self._scan('LPAR') _token_ = self._peek(self.atom_rsts) if _token_ == 'RPAR': v = ListLiteral([], comma=False) elif _token_ not in self.argspec_item_chks: expr_map = self.expr_map() v = expr_map else: # in self.argspec_item_chks expr_lst = self.expr_lst() v = expr_lst RPAR = self._scan('RPAR') return Parentheses(v) elif _token_ == '"url"': self._scan('"url"') LPAR = self._scan('LPAR') _token_ = self._peek(self.atom_rsts_) if _token_ == 'URL': URL = self._scan('URL') quotes = None elif _token_ == '"\\""': self._scan('"\\""') URL = self._scan('URL') self._scan('"\\""') quotes = '"' else: # == '"\'"' self._scan('"\'"') URL = self._scan('URL') self._scan('"\'"') quotes = "'" RPAR = self._scan('RPAR') return Literal(Url(URL, quotes=quotes)) elif _token_ == 'FNCT': FNCT = self._scan('FNCT') LPAR = self._scan('LPAR') argspec = self.argspec() RPAR = self._scan('RPAR') return CallOp(FNCT, argspec) elif _token_ == 'BANG_IMPORTANT': BANG_IMPORTANT = self._scan('BANG_IMPORTANT') return Literal(String(BANG_IMPORTANT, quotes=None)) elif _token_ == 'ID': ID = self._scan('ID') return Literal(parse_bareword(ID)) elif _token_ == 'NUM': NUM = self._scan('NUM') UNITS = None if self._peek(self.atom_rsts__) == 'UNITS': UNITS = self._scan('UNITS') return Literal(Number(float(NUM), unit=UNITS)) elif _token_ == 'STR': STR = self._scan('STR') return Literal(String(dequote(STR), quotes="'")) elif _token_ == 'QSTR': QSTR = self._scan('QSTR') return Literal(String(dequote(QSTR), quotes='"')) elif _token_ == 'COLOR': COLOR = self._scan('COLOR') return Literal(Color.from_hex(COLOR, literal=True)) else: # == 'VAR' VAR = self._scan('VAR') return Variable(VAR)