def get_rest_of_list(buffer: TokenBuffer, end_paren: str): out = [] last = None while ((buffer.get_next_token() != end_paren) and (buffer.get_next_token() != '.')): out.append(get_expression(buffer)) if (buffer.get_next_token() == '.'): buffer.pop_next_token() last = get_expression(buffer) if (buffer.get_next_token() != end_paren): raise ParseError( 'Only one expression may follow a dot in a dotted list.') buffer.pop_next_token() return FormatList(out, last, [], end_paren, True)
def get_expression(buffer: TokenBuffer) -> Formatted: token = buffer.pop_next_token() comments = [] if ((token == '#') and (not buffer.done) and (buffer.get_next_token() == '[')): buffer.pop_next_token() out = FormatAtom((('#[' + buffer.pop_next_token().value) + ']')) buffer.pop_next_token() elif (token in SPECIALS): comments = token.comments if (token in ('(', '[')): out = get_rest_of_list(buffer, (')' if (token == '(') else ']')) elif (token in ("'", '`')): out = get_expression(buffer) out.prefix = token.value elif (token == ','): if (buffer.get_next_token() == '@'): buffer.pop_next_token() out = get_expression(buffer) out.prefix = ',@' else: out = get_expression(buffer) out.prefix = token.value elif (token == '"'): out = FormatAtom((('"' + buffer.pop_next_token().value) + '"')) buffer.pop_next_token() else: raise ParseError(''.join( ["Unexpected token: '", '{}'.format(token), "'"])) else: if (token.value.lower() == 'true'): token.value = '#t' elif (token.value.lower() == 'false'): token.value = '#f' out = FormatAtom(token.value) out.comments = (comments + buffer.tokens[(buffer.i - 1)].comments) out.allow_inline = (token.comments_inline and buffer.tokens[(buffer.i - 1)].comments_inline) return out
def get_expression(buffer: TokenBuffer) -> Formatted: token = buffer.pop_next_token() if isinstance(token, Comment): return FormatComment(token.value, (not token.first_in_line)) elif ((token == '#') and (not buffer.done) and (buffer.get_next_token() == '[')): buffer.pop_next_token() out = FormatAtom((('#[' + buffer.pop_next_token().value) + ']')) buffer.pop_next_token() elif (token in SPECIALS): if (token in ('(', '[')): out = get_rest_of_list(buffer, (')' if (token == '(') else ']')) elif (token in ("'", '`')): out = get_expression(buffer) out.prefix = token.value elif (token == ','): if (buffer.get_next_token() == '@'): buffer.pop_next_token() out = get_expression(buffer) out.prefix = ',@' else: out = get_expression(buffer) out.prefix = token.value elif (token == '"'): out = FormatAtom((('"' + buffer.pop_next_token().value) + '"')) buffer.pop_next_token() else: raise ParseError(''.join( ["Unexpected token: '", '{}'.format(token), "'"])) else: if (token.value.lower() == 'true'): token.value = '#t' elif (token.value.lower() == 'false'): token.value = '#f' out = FormatAtom(token.value) return out
def get_expression(buffer: TokenBuffer) -> Formatted: token = buffer.pop_next_token() if isinstance(token, Comment): return FormatComment(token.value, not token.first_in_line) elif token == "#" and not buffer.done and buffer.get_next_token() == "[": buffer.pop_next_token() out = FormatAtom("#[" + buffer.pop_next_token().value + "]") buffer.pop_next_token() elif token in SPECIALS: if token in ("(", "["): out = get_rest_of_list(buffer, ")" if token == "(" else "]") elif token in ("'", "`"): out = get_expression(buffer) out.prefix = token.value + out.prefix elif token == ",": if buffer.get_next_token() == "@": buffer.pop_next_token() out = get_expression(buffer) out.prefix = ",@" + out.prefix else: out = get_expression(buffer) out.prefix = token.value + out.prefix elif token == "\"": out = FormatAtom('"' + buffer.pop_next_token().value + '"') buffer.pop_next_token() else: raise ParseError(f"Unexpected token: '{token}'") else: if token.value.lower() == "true": token.value = "#t" elif token.value.lower() == "false": token.value = "#f" out = FormatAtom(token.value) return out
def get_expression(buffer: TokenBuffer) -> Union[(Expression, None)]: token = buffer.pop_next_token() if (token is None): return None elif (token in ('(', '[')): return get_rest_of_list(buffer, (')' if (token == '(') else ']')) elif (token == "'"): return make_list([Symbol('quote'), get_expression(buffer)]) elif (token == ','): if (buffer.get_next_token() == '@'): buffer.pop_next_token() return make_list( [Symbol('unquote-splicing'), get_expression(buffer)]) else: return make_list([Symbol('unquote'), get_expression(buffer)]) elif (token == '`'): return make_list([Symbol('quasiquote'), get_expression(buffer)]) elif (token == '.'): if logger.dotted: raise ParseError(''.join( ["Unexpected token: '", '{}'.format(token), "'"])) else: return make_list([Symbol('variadic'), get_expression(buffer)]) elif (token == '"'): return get_string(buffer) elif (token in SPECIALS): raise ParseError(''.join( ["Unexpected token: '", '{}'.format(token), "'"])) elif is_number(token.value): try: return Number(int(token.value)) except ValueError: return Number(float(token.value)) elif ((token == '#t') or (token.value.lower() == 'true')): return SingletonTrue elif ((token == '#f') or (token.value.lower() == 'false')): return SingletonFalse elif (token == 'nil'): return Nil elif is_str(token.value): return Symbol(token.value.lower()) else: raise ParseError(''.join( ["Unexpected token: '", '{}'.format(token), "'"]))
def get_rest_of_list(buffer: TokenBuffer, end_paren: str) -> Expression: out = [] last = Nil while True: next = buffer.get_next_token() if next == end_paren: buffer.pop_next_token() break elif logger.dotted and next == ".": buffer.pop_next_token() last = get_expression(buffer) if buffer.pop_next_token() != end_paren: raise ParseError( f"Only one expression may follow a dot in a dotted list.") break expr = get_expression(buffer) out.append(expr) out = make_list(out, last) return out
def get_expression(buffer: TokenBuffer) -> Union[Expression, None]: token = buffer.pop_next_token() if token is None: return None elif token in ("(", "["): return get_rest_of_list(buffer, ")" if token == "(" else "]") elif token == "'": return make_list([Symbol("quote"), get_expression(buffer)]) elif token == ",": if buffer.get_next_token() == "@": buffer.pop_next_token() return make_list( [Symbol("unquote-splicing"), get_expression(buffer)]) else: return make_list([Symbol("unquote"), get_expression(buffer)]) elif token == "`": return make_list([Symbol("quasiquote"), get_expression(buffer)]) elif token == ".": if logger.dotted: raise ParseError(f"Unexpected token: '{token}'") else: return make_list([Symbol("variadic"), get_expression(buffer)]) elif token == "\"": return get_string(buffer) elif token in SPECIALS: raise ParseError(f"Unexpected token: '{token}'") elif is_number(token.value): try: return Number(int(token.value)) except ValueError: return Number(float(token.value)) elif token == "#t" or token.value.lower() == "true": return SingletonTrue elif token == "#f" or token.value.lower() == "false": return SingletonFalse elif token == "nil": return Nil elif is_str(token.value): return Symbol(token.value.lower()) else: raise ParseError(f"Unexpected token: '{token}'")
def get_rest_of_list(buffer: TokenBuffer, end_paren: str): out = [] while (buffer.get_next_token() != end_paren): out.append(get_expression(buffer)) buffer.pop_next_token() return FormatList(out, end_paren)