-
Notifications
You must be signed in to change notification settings - Fork 0
Implement logos-based tokenizer #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21ac3ff
Add tokenizer using logos
leynos cf35d9f
Add tokenizer tests
leynos 19f9ed1
Add tokenizer mermaid diagram
leynos e2db157
Add module docs and expose tokenizer
leynos 9625b8c
Refine tokenizer tests and dependencies
leynos 23a5207
Improve tokenizer and tests
leynos 373ae27
Add comprehensive tokenizer tests
leynos 6844bf6
Optimise keyword lookup and refine tests
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| //! Library crate for ddlint. | ||
| //! | ||
| //! Currently exposes only the parser language definitions. | ||
| //! Exposes parser language definitions and lexical analysis functionality. | ||
|
|
||
| #![forbid(unsafe_code)] | ||
|
|
||
| pub mod language; | ||
| pub mod tokenizer; | ||
|
|
||
| pub use language::{DdlogLanguage, SyntaxKind}; | ||
| pub use tokenizer::{Span, tokenize}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| //! Lexical analysis for `DDlog` source. | ||
| //! | ||
| //! This module exposes a `tokenize` function which converts raw source text into | ||
| //! a sequence of `(SyntaxKind, Span)` pairs. It uses the `logos` crate to | ||
| //! recognise tokens so that the CST can mirror the input exactly. | ||
|
|
||
| use logos::Logos; | ||
|
|
||
| use crate::SyntaxKind; | ||
|
|
||
| /// Byte range for a token within the source. | ||
| pub type Span = std::ops::Range<usize>; | ||
|
|
||
| #[derive(Logos, Debug, Clone, Copy, PartialEq, Eq)] | ||
| enum Token { | ||
| #[regex(r"[ \t\r\n]+")] | ||
| Whitespace, | ||
| #[regex(r"/\*([^*]|\*[^/])*\*/", priority = 2)] | ||
| #[regex(r"//[^\n]*")] | ||
| Comment, | ||
| #[regex(r"[A-Za-z_][A-Za-z0-9_]*")] | ||
| Ident, | ||
| #[regex(r"[0-9]+")] | ||
| Number, | ||
| #[regex(r#""([^"\\]|\\.)*""#)] | ||
| String, | ||
| #[token("(")] | ||
| LParen, | ||
| #[token(")")] | ||
| RParen, | ||
| #[token("{")] | ||
| LBrace, | ||
| #[token("}")] | ||
| RBrace, | ||
| #[token("[")] | ||
| LBracket, | ||
| #[token("]")] | ||
| RBracket, | ||
| #[token(";")] | ||
| Semi, | ||
| #[token(",")] | ||
| Comma, | ||
| #[token(".")] | ||
| Dot, | ||
| #[token("::")] | ||
| ColonColon, | ||
| #[token(":")] | ||
| Colon, | ||
| #[token("|")] | ||
| Pipe, | ||
| #[token("&")] | ||
| Amp, | ||
| #[token("==")] | ||
| EqEq, | ||
| #[token("=")] | ||
| Eq, | ||
| #[token(":-")] | ||
| Implies, | ||
| #[token("%")] | ||
| Percent, | ||
| #[token("*")] | ||
| Star, | ||
| #[token("/")] | ||
| Slash, | ||
| #[token("+")] | ||
| Plus, | ||
| #[token("-")] | ||
| Minus, | ||
| #[token("->")] | ||
| Arrow, | ||
| #[token("=>")] | ||
| FatArrow, | ||
| #[token("<=")] | ||
| Lte, | ||
| #[token("<=>")] | ||
| Spaceship, | ||
| #[token(">=")] | ||
| Gte, | ||
| #[token("<")] | ||
| Lt, | ||
| #[token(">")] | ||
| Gt, | ||
| #[token("!=")] | ||
| Neq, | ||
| #[token(">>")] | ||
| Shr, | ||
| #[token("<<")] | ||
| Shl, | ||
| #[token("~")] | ||
| Tilde, | ||
| #[token("@")] | ||
| At, | ||
| #[token("#")] | ||
| Hash, | ||
| #[token("'")] | ||
| Apostrophe, | ||
| } | ||
|
|
||
| fn keyword_kind(ident: &str) -> Option<SyntaxKind> { | ||
| Some(match ident { | ||
| "abstract" => SyntaxKind::K_ABSTRACT, | ||
| "Aggregate" => SyntaxKind::K_AGGREGATE, | ||
| "and" => SyntaxKind::K_AND, | ||
| "apply" => SyntaxKind::K_APPLY, | ||
| "as" => SyntaxKind::K_AS, | ||
| "async" => SyntaxKind::K_ASYNC, | ||
| "await" => SyntaxKind::K_AWAIT, | ||
| "become" => SyntaxKind::K_BECOME, | ||
| "bigint" => SyntaxKind::K_BIGINT, | ||
| "bit" => SyntaxKind::K_BIT, | ||
| "bool" => SyntaxKind::K_BOOL, | ||
| "box" => SyntaxKind::K_BOX, | ||
| "break" => SyntaxKind::K_BREAK, | ||
| "const" => SyntaxKind::K_CONST, | ||
| "continue" => SyntaxKind::K_CONTINUE, | ||
| "crate" => SyntaxKind::K_CRATE, | ||
| "do" => SyntaxKind::K_DO, | ||
| "double" => SyntaxKind::K_DOUBLE, | ||
| "dyn" => SyntaxKind::K_DYN, | ||
| "else" => SyntaxKind::K_ELSE, | ||
| "extern" => SyntaxKind::K_EXTERN, | ||
| "false" => SyntaxKind::K_FALSE, | ||
| "final" => SyntaxKind::K_FINAL, | ||
| "fn" => SyntaxKind::K_FN, | ||
| "FlatMap" => SyntaxKind::K_FLATMAP, | ||
| "float" => SyntaxKind::K_FLOAT, | ||
| "for" => SyntaxKind::K_FOR, | ||
| "function" => SyntaxKind::K_FUNCTION, | ||
| "if" => SyntaxKind::K_IF, | ||
| "impl" => SyntaxKind::K_IMPL, | ||
| "import" => SyntaxKind::K_IMPORT, | ||
| "in" => SyntaxKind::K_IN, | ||
| "input" => SyntaxKind::K_INPUT, | ||
| "Inspect" => SyntaxKind::K_INSPECT, | ||
| "let" => SyntaxKind::K_LET, | ||
| "loop" => SyntaxKind::K_LOOP, | ||
| "macro" => SyntaxKind::K_MACRO, | ||
| "match" => SyntaxKind::K_MATCH, | ||
| "mod" => SyntaxKind::K_MOD, | ||
| "move" => SyntaxKind::K_MOVE, | ||
| "multiset" => SyntaxKind::K_MULTISET, | ||
| "mut" => SyntaxKind::K_MUT, | ||
| "not" => SyntaxKind::K_NOT, | ||
| "or" => SyntaxKind::K_OR, | ||
| "override" => SyntaxKind::K_OVERRIDE, | ||
| "output" => SyntaxKind::K_OUTPUT, | ||
| "priv" => SyntaxKind::K_PRIV, | ||
| "pub" => SyntaxKind::K_PUB, | ||
| "ref" => SyntaxKind::K_REF, | ||
| "relation" => SyntaxKind::K_RELATION, | ||
| "return" => SyntaxKind::K_RETURN, | ||
| "self" => SyntaxKind::K_SELF, | ||
| "Self" => SyntaxKind::K_SELF_TYPE, | ||
| "signed" => SyntaxKind::K_SIGNED, | ||
| "skip" => SyntaxKind::K_SKIP, | ||
| "static" => SyntaxKind::K_STATIC, | ||
| "stream" => SyntaxKind::K_STREAM, | ||
| "struct" => SyntaxKind::K_STRUCT, | ||
| "super" => SyntaxKind::K_SUPER, | ||
| "trait" => SyntaxKind::K_TRAIT, | ||
| "transformer" => SyntaxKind::K_TRANSFORMER, | ||
| "try" => SyntaxKind::K_TRY, | ||
| "true" => SyntaxKind::K_TRUE, | ||
| "type" => SyntaxKind::K_TYPE, | ||
| "typedef" => SyntaxKind::K_TYPEDEF, | ||
| "typeof" => SyntaxKind::K_TYPEOF, | ||
| "_" => SyntaxKind::K_UNDERSCORE, | ||
| "unsafe" => SyntaxKind::K_UNSAFE, | ||
| "unsized" => SyntaxKind::K_UNSIZED, | ||
| "use" => SyntaxKind::K_USE, | ||
| "var" => SyntaxKind::K_VAR, | ||
| "virtual" => SyntaxKind::K_VIRTUAL, | ||
| "where" => SyntaxKind::K_WHERE, | ||
| "while" => SyntaxKind::K_WHILE, | ||
| "yield" => SyntaxKind::K_YIELD, | ||
| _ => return None, | ||
| }) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /// Tokenise the provided `DDlog` source. | ||
| #[must_use] | ||
| pub fn tokenize(src: &str) -> Vec<(SyntaxKind, Span)> { | ||
| let mut lexer = Token::lexer(src); | ||
| let mut out = Vec::new(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| while let Some(result) = lexer.next() { | ||
| let span = lexer.span(); | ||
| let text = src.get(span.clone()).unwrap_or(""); | ||
| let Ok(token) = result else { | ||
| out.push((SyntaxKind::N_ERROR, span)); | ||
| continue; | ||
| }; | ||
| let kind = match token { | ||
| Token::Whitespace => SyntaxKind::T_WHITESPACE, | ||
| Token::Comment => SyntaxKind::T_COMMENT, | ||
| Token::Ident => keyword_kind(text).unwrap_or(SyntaxKind::T_IDENT), | ||
| Token::Number => SyntaxKind::T_NUMBER, | ||
| Token::String => SyntaxKind::T_STRING, | ||
| Token::LParen => SyntaxKind::T_LPAREN, | ||
| Token::RParen => SyntaxKind::T_RPAREN, | ||
| Token::LBrace => SyntaxKind::T_LBRACE, | ||
| Token::RBrace => SyntaxKind::T_RBRACE, | ||
| Token::LBracket => SyntaxKind::T_LBRACKET, | ||
| Token::RBracket => SyntaxKind::T_RBRACKET, | ||
| Token::Semi => SyntaxKind::T_SEMI, | ||
| Token::Comma => SyntaxKind::T_COMMA, | ||
| Token::Dot => SyntaxKind::T_DOT, | ||
| Token::ColonColon => SyntaxKind::T_COLON_COLON, | ||
| Token::Colon => SyntaxKind::T_COLON, | ||
| Token::Pipe => SyntaxKind::T_PIPE, | ||
| Token::Amp => SyntaxKind::T_AMP, | ||
| Token::EqEq => SyntaxKind::T_EQEQ, | ||
| Token::Eq => SyntaxKind::T_EQ, | ||
| Token::Implies => SyntaxKind::T_IMPLIES, | ||
| Token::Percent => SyntaxKind::T_PERCENT, | ||
| Token::Star => SyntaxKind::T_STAR, | ||
| Token::Slash => SyntaxKind::T_SLASH, | ||
| Token::Plus => SyntaxKind::T_PLUS, | ||
| Token::Minus => SyntaxKind::T_MINUS, | ||
| Token::Arrow => SyntaxKind::T_ARROW, | ||
| Token::FatArrow => SyntaxKind::T_FAT_ARROW, | ||
| Token::Lte => SyntaxKind::T_LTE, | ||
| Token::Spaceship => SyntaxKind::T_SPACESHIP, | ||
| Token::Gte => SyntaxKind::T_GTE, | ||
| Token::Lt => SyntaxKind::T_LT, | ||
| Token::Gt => SyntaxKind::T_GT, | ||
| Token::Neq => SyntaxKind::T_NEQ, | ||
| Token::Shr => SyntaxKind::T_SHR, | ||
| Token::Shl => SyntaxKind::T_SHL, | ||
| Token::Tilde => SyntaxKind::T_TILDE, | ||
| Token::At => SyntaxKind::T_AT, | ||
| Token::Hash => SyntaxKind::T_HASH, | ||
| Token::Apostrophe => SyntaxKind::T_APOSTROPHE, | ||
| }; | ||
| out.push((kind, span)); | ||
| } | ||
| out | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.