Better hex literals

This commit is contained in:
greg 2018-01-08 06:12:45 -08:00
parent 2bff53846c
commit 95c6a23bf1

View File

@ -18,7 +18,7 @@ pub enum TokenType {
Comma, Period, Colon, Underscore, Comma, Period, Colon, Underscore,
Operator(Rc<String>), Operator(Rc<String>),
DigitGroup(Rc<String>), HexNumberSigil, BinNumberSigil, DigitGroup(Rc<String>), HexLiteral(Rc<String>), BinNumberSigil,
StrLiteral(Rc<String>), StrLiteral(Rc<String>),
Identifier(Rc<String>), Identifier(Rc<String>),
Keyword(Kw), Keyword(Kw),
@ -132,7 +132,8 @@ pub fn tokenize(input: &str) -> Vec<Token> {
fn handle_digit(c: char, input: &mut CharIter) -> TokenType { fn handle_digit(c: char, input: &mut CharIter) -> TokenType {
if c == '0' && input.peek().map_or(false, |&(_, c)| { c == 'x' }) { if c == '0' && input.peek().map_or(false, |&(_, c)| { c == 'x' }) {
input.next(); input.next();
HexNumberSigil let rest: String = input.peeking_take_while(|&(_, ref c)| c.is_digit(16) || *c == '_').map(|(_, c)| { c }).collect();
HexLiteral(Rc::new(rest))
} else if c == '0' && input.peek().map_or(false, |&(_, c)| { c == 'b' }) { } else if c == '0' && input.peek().map_or(false, |&(_, c)| { c == 'b' }) {
input.next(); input.next();
BinNumberSigil BinNumberSigil
@ -917,7 +918,7 @@ impl Parser {
parse_method!(literal(&mut self) -> ParseResult<Expression> { parse_method!(literal(&mut self) -> ParseResult<Expression> {
use self::ExpressionType::*; use self::ExpressionType::*;
match self.peek() { match self.peek() {
DigitGroup(_) | HexNumberSigil | BinNumberSigil | Period => self.number_literal(), DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(),
Keyword(Kw::True) => { Keyword(Kw::True) => {
self.next(); self.next();
Ok(Expression(BoolLiteral(true), None)) Ok(Expression(BoolLiteral(true), None))
@ -936,7 +937,7 @@ impl Parser {
parse_method!(number_literal(&mut self) -> ParseResult<Expression> { parse_method!(number_literal(&mut self) -> ParseResult<Expression> {
match self.peek() { match self.peek() {
HexNumberSigil | BinNumberSigil => self.int_literal(), HexLiteral(_) | BinNumberSigil => self.int_literal(),
_ => self.float_literal(), _ => self.float_literal(),
} }
}); });
@ -949,7 +950,7 @@ impl Parser {
let n = parse_binary(digits)?; let n = parse_binary(digits)?;
Ok(Expression(IntLiteral(n), None)) Ok(Expression(IntLiteral(n), None))
}, },
HexNumberSigil => { HexLiteral(text) => {
ParseError::new("Not implemented") ParseError::new("Not implemented")
}, },
_ => return ParseError::new("Expected '0x' or '0b'"), _ => return ParseError::new("Expected '0x' or '0b'"),