From f88351288246258321ed5d8a15f693521bb8e042 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 21 Oct 2018 16:33:21 -0700 Subject: [PATCH] New abstraction layer in Schala-lang parser Just for manipulating tokens --- schala-lang/language/src/parsing.rs | 39 ++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 78ae9ad..b0c75f5 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -35,7 +35,7 @@ pub struct ParseRecord { } pub struct Parser { - tokens: Peekable>, + token_handler: TokenHandler, parse_record: Vec, parse_level: u32, restrictions: ParserRestrictions, @@ -45,14 +45,14 @@ struct ParserRestrictions { no_struct_literal: bool } -impl Parser { - pub fn new(initial_input: Vec) -> Parser { - Parser { - tokens: initial_input.into_iter().peekable(), - parse_record: vec![], - parse_level: 0, - restrictions: ParserRestrictions { no_struct_literal: false } - } +struct TokenHandler { + tokens: Peekable>, +} + +impl TokenHandler { + fn new(tokens: Vec) -> TokenHandler { + let tokens = tokens.into_iter().peekable(); + TokenHandler { tokens } } fn peek(&mut self) -> TokenType { @@ -64,6 +64,27 @@ impl Parser { fn next(&mut self) -> TokenType { self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF) } +} + +impl Parser { + pub fn new(initial_input: Vec) -> 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 { self.program()