Skip to content

Commit

Permalink
MWE of the lexer works! #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 20, 2018
1 parent 1d0c8d5 commit f79c75b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
19 changes: 9 additions & 10 deletions src/mango/lexing/code_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use mango::util::strslice::char_ops::CharOps;
use mango::util::strslice::charsliceto;

pub struct CodeLexer {
indent: i32,
indent: i32, // -1: finished
}

// TODO: keep the regexes in thread local global scope storage
Expand Down Expand Up @@ -141,12 +141,6 @@ impl SubLexer for CodeLexer {
return SubLexerResult::single(Tokens::Literal(LiteralToken::Real(value)));
}

// Operator (before association)
if let Match(token) = reader.matches(OperatorToken::subpattern()) {
return SubLexerResult::single(Tokens::Operator(
OperatorToken::from_str(&token).unwrap(),
));
}
// Association (before operator)
if let Match(token) = reader.matches(&AssociationToken::subpattern()) {
debug_assert!(token.chars().last().unwrap() == '=');
Expand All @@ -163,6 +157,12 @@ impl SubLexer for CodeLexer {
);
}
}
// Operator (after association)
if let Match(token) = reader.matches(OperatorToken::subpattern()) {
return SubLexerResult::single(Tokens::Operator(
OperatorToken::from_str(&token).unwrap(),
));
}
// Grouping symbols
if let Match(_) = reader.matches(r"\(") {
return SubLexerResult::single(Tokens::ParenthesisOpen(ParenthesisOpenToken::new()));
Expand All @@ -176,16 +176,15 @@ impl SubLexer for CodeLexer {
Match(word) => SubLexerResult::single(Tokens::Unlexable(UnlexableToken::new(word))),
NoMatch() => panic!("Do not know how to proceed with parsing"),
EOF() => {
if self.indent <= 0 {
if self.indent < 0 {
return SubLexerResult::End;
}
// TODO: currently the EndStatement is only made if the file stops on an indented line
let mut tokens = vec![Tokens::EndStatement(EndStatementToken::new_end_line())];
for _ in 0..self.indent {
// This line is dedented, make end tokens.
tokens.push(Tokens::EndBlock(EndBlockToken::new(true, false)));
}
self.indent = 0;
self.indent = -1;
SubLexerResult::Result(tokens)
}
};
Expand Down
12 changes: 5 additions & 7 deletions src/mango/lexing/combi_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ impl Lexer for CombiLexer {
match lexer.lex_pass(&mut self.reader) {
SubLexerResult::Result(tokens) => {
if tokens.len() > 0 {
if tokens.len() > 1 {
// TODO
println!(">> GOING TO ADD: {:?}", tokens);
}
// The sublexer produced tokens, queue them.
self.buffer.append(tokens);
self.lex() // TODO: if every branch does this, move it down
Expand Down Expand Up @@ -107,9 +103,10 @@ mod tests {
fn test_lexing_individual() {
assert_text_to_tokens(
"if",
vec![Tokens::Keyword(
KeywordToken::from_str("if".to_owned()).unwrap(),
)],
vec![
Tokens::Keyword(KeywordToken::from_str("if".to_owned()).unwrap()),
Tokens::EndStatement(EndStatementToken::new_end_line()),
],
);
// todo: more
}
Expand All @@ -133,6 +130,7 @@ mod tests {
Tokens::Identifier(IdentifierToken::from_str("x".to_owned()).unwrap()),
Tokens::Association(AssociationToken::from_str("+".to_owned()).unwrap()),
Tokens::Literal(LiteralToken::Int(1)),
Tokens::EndStatement(EndStatementToken::new_end_line()),
Tokens::EndBlock(EndBlockToken::new(true, false)),
],
);
Expand Down

0 comments on commit f79c75b

Please sign in to comment.