Improve tokenizer debug output
This commit is contained in:
parent
66cd51a355
commit
9ab1ca28f8
@ -264,7 +264,7 @@ macro_rules! parse_method {
|
|||||||
let next_token = $self.peek_with_token_offset();
|
let next_token = $self.peek_with_token_offset();
|
||||||
let record = ParseRecord {
|
let record = ParseRecord {
|
||||||
production_name: stringify!($name).to_string(),
|
production_name: stringify!($name).to_string(),
|
||||||
next_token: format!("{:?}", next_token),
|
next_token: format!("{}", next_token.to_string_with_metadata()),
|
||||||
level: $self.parse_level,
|
level: $self.parse_level,
|
||||||
};
|
};
|
||||||
$self.parse_level += 1;
|
$self.parse_level += 1;
|
||||||
@ -525,10 +525,9 @@ impl Parser {
|
|||||||
|
|
||||||
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
||||||
fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {
|
fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {
|
||||||
let next_token = self.peek();
|
|
||||||
let record = ParseRecord {
|
let record = ParseRecord {
|
||||||
production_name: "precedence_expr".to_string(),
|
production_name: "precedence_expr".to_string(),
|
||||||
next_token: format!("{:?}", next_token),
|
next_token: format!("{}", self.peek_with_token_offset().to_string_with_metadata()),
|
||||||
level: self.parse_level,
|
level: self.parse_level,
|
||||||
};
|
};
|
||||||
self.parse_level += 1;
|
self.parse_level += 1;
|
||||||
@ -830,7 +829,7 @@ pub fn parse(input: Vec<Token>) -> (Result<AST, ParseError>, Vec<String>) {
|
|||||||
for _ in 0..r.level {
|
for _ in 0..r.level {
|
||||||
indent.push(' ');
|
indent.push(' ');
|
||||||
}
|
}
|
||||||
format!("{}Production `{}`, token: {:?}", indent, r.production_name, r.next_token)
|
format!("{}Production `{}`, token: {}", indent, r.production_name, r.next_token)
|
||||||
}).collect();
|
}).collect();
|
||||||
(ast, trace)
|
(ast, trace)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ use std::collections::HashMap;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::iter::{Iterator, Enumerate, Peekable, FlatMap};
|
use std::iter::{Iterator, Enumerate, Peekable, FlatMap};
|
||||||
use std::str::{Lines, Chars};
|
use std::str::{Lines, Chars};
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum TokenType {
|
pub enum TokenType {
|
||||||
@ -28,6 +30,20 @@ pub enum TokenType {
|
|||||||
}
|
}
|
||||||
use self::TokenType::*;
|
use self::TokenType::*;
|
||||||
|
|
||||||
|
impl fmt::Display for TokenType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
&Operator(ref s) => write!(f, "Operator({})", **s),
|
||||||
|
&DigitGroup(ref s) => write!(f, "DigitGroup({})", s),
|
||||||
|
&HexLiteral(ref s) => write!(f, "HexLiteral({})", s),
|
||||||
|
&StrLiteral(ref s) => write!(f, "StrLiteral({})", s),
|
||||||
|
&Identifier(ref s) => write!(f, "Identifier({})", s),
|
||||||
|
&Error(ref s) => write!(f, "Error({})", s),
|
||||||
|
other => write!(f, "{:?}", other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Kw {
|
pub enum Kw {
|
||||||
If, Else,
|
If, Else,
|
||||||
@ -80,6 +96,12 @@ impl Token {
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn to_string(&self) -> String {
|
||||||
|
format!("{}", self.token_type)
|
||||||
|
}
|
||||||
|
pub fn to_string_with_metadata(&self) -> String {
|
||||||
|
format!("{}(L:{},c:{})", self.token_type, self.offset.0, self.offset.1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const OPERATOR_CHARS: [char; 19] = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<', '>', '=', '?', '@', '^', '|', '~', '`'];
|
const OPERATOR_CHARS: [char; 19] = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<', '>', '=', '?', '@', '^', '|', '~', '`'];
|
||||||
|
@ -118,7 +118,7 @@ impl TypeContext {
|
|||||||
pub fn debug_symbol_table(&self) -> String {
|
pub fn debug_symbol_table(&self) -> String {
|
||||||
let mut output = format!("Symbols\n");
|
let mut output = format!("Symbols\n");
|
||||||
for (sym, ty) in &self.bindings {
|
for (sym, ty) in &self.bindings {
|
||||||
write!(output, "{} : {}\n", sym, ty);
|
write!(output, "{} : {}\n", sym, ty).unwrap();
|
||||||
}
|
}
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user