Skip to content

Commit

Permalink
Resolved technical errors, only lexing problems remain #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 19, 2018
1 parent 0195c7a commit 089c17e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/mango/lexing/code_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use mango::token::tokens::ParenthesisOpenToken;
use mango::token::tokens::StartBlockToken;
use mango::token::Tokens;
use mango::util::collection::Queue;
use mango::util::strslice::char_ops::CharOps;
use mango::util::strslice::charsliceto;

pub struct CodeLexer {
indent: i32,
Expand Down Expand Up @@ -145,11 +147,13 @@ impl SubLexer for CodeLexer {
// Association (before operator)
if let Match(token) = reader.matches(&AssociationToken::subpattern()) {
debug_assert!(token.chars().last().unwrap() == '=');
if token.chars().count() > 1 {
panic!(); // TODO
return SubLexerResult::single(
(Tokens::Association(AssociationToken::from_unprefixed())),
);
if token.char_len() > 1 {
match AssociationToken::from_str(charsliceto(token, -1)) {
Ok(association) => {
return SubLexerResult::single((Tokens::Association(association)))
}
Err(msg) => panic!(format!("Invalid association prefix: {}", msg)),
}
} else {
return SubLexerResult::single(
(Tokens::Association(AssociationToken::from_unprefixed())),
Expand All @@ -173,10 +177,7 @@ impl SubLexer for CodeLexer {
// If the code gets here, it did not recognize the text as any token
return match reader.matches(r"[^\s]+") {
Match(word) => SubLexerResult::single(Tokens::Unlexable(UnlexableToken::new(word))),
NoMatch() => {
println!("END {:?}", reader); // todo: tmp
panic!("Do not know how to proceed with parsing")
}
NoMatch() => panic!("Do not know how to proceed with parsing"),
EOF() => {
if self.indent <= 0 {
return SubLexerResult::End;
Expand Down
17 changes: 12 additions & 5 deletions src/mango/lexing/combi_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,22 @@ mod tests {
use mango::token::tokens::ParenthesisOpenToken;
use mango::token::tokens::StartBlockToken;
use mango::token::Tokens;
use mango::util::encdec::to_text::ToText;
use std::cell::RefCell;
use std::rc::Rc;

fn assert_text_to_tokens(text: &str, tokens: Vec<Tokens>) {
let expected = LexList::from_tokens(tokens);
let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new(
text.to_owned(),
))));
assert_eq!(
LexList::from_tokens(tokens),
lex_all(&mut CombiLexer::new(Box::new(StringReader::new(
text.to_owned()
))))
)
expected,
actual,
"expected: {}\nactual: {}",
expected.to_text(),
actual.to_text(),
);
}

#[test]
Expand All @@ -115,6 +121,7 @@ mod tests {
Tokens::Literal(LiteralToken::Int(0)),
Tokens::EndStatement(EndStatementToken::new_end_line()),
Tokens::Keyword(KeywordToken::from_str("for".to_owned()).unwrap()),
Tokens::Identifier(IdentifierToken::from_str("x".to_owned()).unwrap()),
Tokens::Operator(OperatorToken::from_str("<").unwrap()),
Tokens::Literal(LiteralToken::Int(128)),
Tokens::EndStatement(EndStatementToken::new_end_line()),
Expand Down
11 changes: 11 additions & 0 deletions src/mango/lexing/util/lex_all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mango::lexing::typ::Lexer;
use mango::lexing::typ::MaybeToken;
use mango::token::Tokens;
use mango::util::encdec::ToText;

/// Represents all the lex tokens in a source.
#[derive(PartialEq, Eq, Debug)]
Expand All @@ -18,6 +19,16 @@ impl LexList {
}
}

impl ToText for LexList {
fn to_text(&self) -> String {
self.tokens
.iter()
.map(|token| token.to_text())
.collect::<Vec<_>>()
.join(" ")
}
}

pub fn lex_all(lexer: &mut Lexer) -> LexList {
let mut list = Vec::with_capacity(512);
while let MaybeToken::Token(token) = lexer.lex() {
Expand Down
2 changes: 1 addition & 1 deletion src/mango/token/collect/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ mod tests {

#[test]
fn test_tokens_size() {
assert!(size_of::<Tokens>() < 32, size_of::<Tokens>());
assert!(size_of::<Tokens>() <= 40, size_of::<Tokens>());
}
}
2 changes: 1 addition & 1 deletion src/mango/token/tokens/association.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl AssociationToken {
}

pub fn subpattern() -> String {
format!("{}=", Symbol::subpattern())
format!(r"(?:{})?=", Symbol::subpattern())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mango/util/codeparts/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Symbol {

/// Generate an eager subpattern to match tokens, that can be composed in a regular expression.
pub fn subpattern() -> &'static str {
r"(\+|\-|\*|/)"
r"[\-+*/]"
}
}

Expand Down

0 comments on commit 089c17e

Please sign in to comment.