New abstraction layer in Schala-lang parser

Just for manipulating tokens
This commit is contained in:
greg 2018-10-21 16:33:21 -07:00
parent 37070a6b3e
commit f883512882
1 changed files with 30 additions and 9 deletions

View File

@ -35,7 +35,7 @@ pub struct ParseRecord {
} }
pub struct Parser { pub struct Parser {
tokens: Peekable<IntoIter<Token>>, token_handler: TokenHandler,
parse_record: Vec<ParseRecord>, parse_record: Vec<ParseRecord>,
parse_level: u32, parse_level: u32,
restrictions: ParserRestrictions, restrictions: ParserRestrictions,
@ -45,14 +45,14 @@ struct ParserRestrictions {
no_struct_literal: bool no_struct_literal: bool
} }
impl Parser { struct TokenHandler {
pub fn new(initial_input: Vec<Token>) -> Parser { tokens: Peekable<IntoIter<Token>>,
Parser { }
tokens: initial_input.into_iter().peekable(),
parse_record: vec![], impl TokenHandler {
parse_level: 0, fn new(tokens: Vec<Token>) -> TokenHandler {
restrictions: ParserRestrictions { no_struct_literal: false } let tokens = tokens.into_iter().peekable();
} TokenHandler { tokens }
} }
fn peek(&mut self) -> TokenType { fn peek(&mut self) -> TokenType {
@ -64,6 +64,27 @@ impl Parser {
fn next(&mut self) -> TokenType { fn next(&mut self) -> TokenType {
self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
} }
}
impl Parser {
pub fn new(initial_input: Vec<Token>) -> Parser {
Parser {
token_handler: TokenHandler::new(initial_input),
parse_record: vec![],
parse_level: 0,
restrictions: ParserRestrictions { no_struct_literal: false }
}
}
fn peek(&mut self) -> TokenType {
self.token_handler.peek()
}
fn peek_with_token_offset(&mut self) -> Token {
self.token_handler.peek_with_token_offset()
}
fn next(&mut self) -> TokenType {
self.token_handler.next()
}
pub fn parse(&mut self) -> ParseResult<AST> { pub fn parse(&mut self) -> ParseResult<AST> {
self.program() self.program()