WIP source map stuff

This commit is contained in:
greg 2018-11-16 04:12:07 -08:00
parent e42f0c644c
commit d357876b16
2 changed files with 38 additions and 19 deletions

View File

@ -6,6 +6,7 @@ use tokenizing::*;
use tokenizing::Kw::*;
use tokenizing::TokenType::*;
use source_map::{SourceMap, SourceData};
use ast::*;
use builtin::{BinOp, PrefixOp};
@ -90,8 +91,12 @@ impl Parser {
self.token_handler.next()
}
fn next_with_metadata(&mut self) -> Token {
self.token_handler.next_with_metadata()
fn next_with_map(&mut self) -> SourceMap<TokenType> {
let tt = self.next();
SourceMap {
node: tt,
data: SourceData { line_number: 420, char_idx: 69 }
}
}
pub fn parse(&mut self) -> ParseResult<AST> {
@ -1023,21 +1028,24 @@ impl Parser {
#[recursive_descent_method]
fn int_literal(&mut self) -> ParseResult<Expression> {
use self::ExpressionType::*;
match self.next_with_metadata().get_token_type() {
BinNumberSigil => {
let digits = self.digits()?;
let n = parse_binary(digits)?;
Ok(Expression(NatLiteral(n), None))
},
HexLiteral(text) => {
let digits: String = text.chars().filter(|c| c.is_digit(16)).collect();
let n = parse_hex(digits)?;
Ok(Expression(NatLiteral(n), None))
},
_ => return ParseError::new("Expected '0x' or '0b'"),
match self.next_with_map() {
t => match t.get() {
BinNumberSigil => {
let digits = self.digits()?;
let n = parse_binary(digits)?;
Ok(Expression(NatLiteral(n), None))
},
HexLiteral(text) => {
let digits: String = text.chars().filter(|c| c.is_digit(16)).collect();
let n = parse_hex(digits)?;
Ok(Expression(NatLiteral(n), None))
},
_ => return ParseError::new("Expected '0x' or '0b'"),
}
}
}
#[recursive_descent_method]
fn float_literal(&mut self) -> ParseResult<Expression> {
use self::ExpressionType::*;

View File

@ -1,12 +1,23 @@
#[derive(Debug, Clone)]
pub struct SourceMap<T> {
node: T,
data: SourceData
pub node: T,
pub data: SourceData
}
struct SourceData {
line_number: usize,
char_idx: usize
impl<T> SourceMap<T> {
pub fn get(&self) -> &T {
&self.node
}
pub fn get_source_data(&self) -> SourceData {
So
}
#[derive(Debug, Clone)]
pub struct SourceData {
pub line_number: usize,
pub char_idx: usize
}