| 1 | //! Simple hand-written ungrammar lexer |
| 2 | use craterust-analyzer cargo ungrammar 1.16.1 crate/::errorrust-analyzer cargo ungrammar 1.16.1 error/::{bailrust-analyzer cargo ungrammar 1.16.1 error/_bail!, Resultrust-analyzer cargo ungrammar 1.16.1 error/Result#}; |
| 3 | |
| 4 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 5 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) enum TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind# { |
| 6 | Noderust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Node#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 7 | Tokenrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Token#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 8 | Eqrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Eq#, |
| 9 | Starrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Star#, |
| 10 | Piperust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Pipe#, |
| 11 | QMarkrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#QMark#, |
| 12 | Colonrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Colon#, |
| 13 | LParenrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#LParen#, |
| 14 | RParenrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#RParen#, |
| 15 | } |
| 16 | |
| 17 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 18 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) struct Tokenrust-analyzer cargo ungrammar 1.16.1 lexer/Token# { |
| 19 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) kindrust-analyzer cargo ungrammar 1.16.1 lexer/Token#kind.: TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#, |
| 20 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) locrust-analyzer cargo ungrammar 1.16.1 lexer/Token#loc.: Locationrust-analyzer cargo ungrammar 1.16.1 lexer/Location#, |
| 21 | } |
| 22 | |
| 23 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 24 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) struct Locationrust-analyzer cargo ungrammar 1.16.1 lexer/Location# { |
| 25 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) linerust-analyzer cargo ungrammar 1.16.1 lexer/Location#line.: usize, |
| 26 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) columnrust-analyzer cargo ungrammar 1.16.1 lexer/Location#column.: usize, |
| 27 | } |
| 28 | |
| 29 | impl Locationrust-analyzer cargo ungrammar 1.16.1 lexer/Location# { |
| 30 | fn advancerust-analyzer cargo ungrammar 1.16.1 lexer/impl#[Location]advance().(&mut self, text: &str) { |
| 31 | match text.rfindrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rfind().('\n') { |
| 32 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(idx) => { |
| 33 | self.linerust-analyzer cargo ungrammar 1.16.1 lexer/Location#line. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().text.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|&it| it rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\n').countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]count().(); |
| 34 | self.columnrust-analyzer cargo ungrammar 1.16.1 lexer/Location#column. = text[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(); |
| 35 | } |
| 36 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => self.columnrust-analyzer cargo ungrammar 1.16.1 lexer/Location#column. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().text.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(), |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub(craterust-analyzer cargo ungrammar 1.16.1 crate/) fn tokenizerust-analyzer cargo ungrammar 1.16.1 lexer/tokenize().(mut input: &str) -> Resultrust-analyzer cargo ungrammar 1.16.1 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Tokenrust-analyzer cargo ungrammar 1.16.1 lexer/Token#>> { |
| 42 | let mut res = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 43 | let mut loc = Locationrust-analyzer cargo ungrammar 1.16.1 lexer/Location#::defaultrust-analyzer cargo ungrammar 1.16.1 lexer/impl#[Location][Default]default().(); |
| 44 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().input.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 45 | let old_input = input; |
| 46 | skip_wsrust-analyzer cargo ungrammar 1.16.1 lexer/skip_ws().(&mut input); |
| 47 | skip_commentrust-analyzer cargo ungrammar 1.16.1 lexer/skip_comment().(&mut input); |
| 48 | if old_input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() { |
| 49 | match advancerust-analyzer cargo ungrammar 1.16.1 lexer/advance().(&mut input) { |
| 50 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(kind) => { |
| 51 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Tokenrust-analyzer cargo ungrammar 1.16.1 lexer/Token# { kindrust-analyzer cargo ungrammar 1.16.1 lexer/Token#kind., locrust-analyzer cargo ungrammar 1.16.1 lexer/Token#loc. }); |
| 52 | } |
| 53 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err.with_locationrust-analyzer cargo ungrammar 1.16.1 error/impl#[Error]with_location().(loc)), |
| 54 | } |
| 55 | } |
| 56 | let consumed = old_input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 57 | loc.advancerust-analyzer cargo ungrammar 1.16.1 lexer/impl#[Location]advance().(&old_input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#consumed]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 58 | } |
| 59 | |
| 60 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) |
| 61 | } |
| 62 | |
| 63 | fn skip_wsrust-analyzer cargo ungrammar 1.16.1 lexer/skip_ws().(input: &mut &str) { |
| 64 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().input = input.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(is_whitespacerust-analyzer cargo ungrammar 1.16.1 lexer/is_whitespace().) |
| 65 | } |
| 66 | fn skip_commentrust-analyzer cargo ungrammar 1.16.1 lexer/skip_comment().(input: &mut &str) { |
| 67 | if input.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("//") { |
| 68 | let idx = input.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('\n').map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(), |it| it rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 69 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().input = &input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn advancerust-analyzer cargo ungrammar 1.16.1 lexer/advance().(input: &mut &str) -> Resultrust-analyzer cargo ungrammar 1.16.1 error/Result#<TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#> { |
| 74 | let mut chars = input.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().(); |
| 75 | let c = chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 76 | let res = match c { |
| 77 | '=' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::Eqrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Eq#, |
| 78 | '*' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::Starrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Star#, |
| 79 | '?' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::QMarkrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#QMark#, |
| 80 | '(' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::LParenrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#LParen#, |
| 81 | ')' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::RParenrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#RParen#, |
| 82 | '|' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::Piperust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Pipe#, |
| 83 | ':' => TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::Colonrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Colon#, |
| 84 | '\'' => { |
| 85 | let mut buf = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 86 | loop { |
| 87 | match chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 88 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => bailrust-analyzer cargo ungrammar 1.16.1 error/_bail!!("unclosed token literal"), |
| 89 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('\\') => match chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 90 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) if is_escapablerust-analyzer cargo ungrammar 1.16.1 lexer/is_escapable().(c) => buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c), |
| 91 | _ => bailrust-analyzer cargo ungrammar 1.16.1 error/_bail!!("invalid escape in token literal"), |
| 92 | }, |
| 93 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('\'') => break, |
| 94 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) => buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c), |
| 95 | } |
| 96 | } |
| 97 | TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::Tokenrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Token#(buf) |
| 98 | } |
| 99 | c if is_ident_charrust-analyzer cargo ungrammar 1.16.1 lexer/is_ident_char().(c) => { |
| 100 | let mut buf = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 101 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c); |
| 102 | loop { |
| 103 | match chars.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Clone]clone().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 104 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) if is_ident_charrust-analyzer cargo ungrammar 1.16.1 lexer/is_ident_char().(c) => { |
| 105 | chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().(); |
| 106 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c); |
| 107 | } |
| 108 | _ => break, |
| 109 | } |
| 110 | } |
| 111 | TokenKindrust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#::Noderust-analyzer cargo ungrammar 1.16.1 lexer/TokenKind#Node#(buf) |
| 112 | } |
| 113 | '\r' => bailrust-analyzer cargo ungrammar 1.16.1 error/_bail!!("unexpected `\\r`, only Unix-style line endings allowed"), |
| 114 | c => bailrust-analyzer cargo ungrammar 1.16.1 error/_bail!!("unexpected character: `{}`", c), |
| 115 | }; |
| 116 | |
| 117 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().input = chars.as_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`]as_str().(); |
| 118 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) |
| 119 | } |
| 120 | |
| 121 | fn is_escapablerust-analyzer cargo ungrammar 1.16.1 lexer/is_escapable().(c: char) -> bool { |
| 122 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, '\\' | '\'') |
| 123 | } |
| 124 | fn is_whitespacerust-analyzer cargo ungrammar 1.16.1 lexer/is_whitespace().(c: char) -> bool { |
| 125 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, ' ' | '\t' | '\n') |
| 126 | } |
| 127 | fn is_ident_charrust-analyzer cargo ungrammar 1.16.1 lexer/is_ident_char().(c: char) -> bool { |
| 128 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, 'a'..='z' | 'A'..='Z' | '_') |
| 129 | } |