fix(lexer): stall on bad characters

This commit is contained in:
Chen Asraf
2022-08-16 03:08:49 +03:00
parent 188bac3502
commit ee8f8aa3ab
2 changed files with 15 additions and 10 deletions

View File

@@ -51,8 +51,8 @@ const result = [
**Input:** `(mango banana lemon) OR apple -pineapple` **Input:** `(mango banana lemon) OR apple -pineapple`
**Explanation:** One of the word: "mango", "banana", "lemon" OR "apple"; exclude all results **Explanation:** Either one of the words: "mango", "banana", or "lemon"; OR the word "apple";
containing "pineapple" exclude all results containing "pineapple"
**Output**: **Output**:
@@ -108,7 +108,8 @@ This is the comprehensive list of operators and their object results:
- **Word:** `example` - **Word:** `example`
Any single word. Only alpha-numeric characters, dashes and underscores are considered a word. The Any single word. Only alpha-numeric characters, dashes and underscores are considered a word. The
rest is ignored. rest is considered whitespace, which is ignored by the parser, but will cause the surrounding
tokens to be broken apart.
**Object:** **Object:**

View File

@@ -110,11 +110,7 @@ export class Lexer implements ILexer {
case lexerState.default: case lexerState.default:
// whitespace // whitespace
if (this.isWhitespace(nextChar)) { if (this.isWhitespace(nextChar)) {
this.afterWhitespace = true return this.consumeWhitespace()
return {
value: this.reader.consume(),
token: LexerToken.whitespace,
}
} }
// quote // quote
@@ -154,8 +150,8 @@ export class Lexer implements ILexer {
return this.consumeGroup() return this.consumeGroup()
} }
// other, consume normally // other, consider as whitespace
return this.consumeWord() return this.consumeWhitespace()
case lexerState.inPhrase: case lexerState.inPhrase:
this.afterWhitespace = false this.afterWhitespace = false
@@ -172,6 +168,14 @@ export class Lexer implements ILexer {
} }
} }
private consumeWhitespace() {
this.afterWhitespace = true
return {
value: this.reader.consume(),
token: LexerToken.whitespace,
}
}
private consumeQuote(): LexerTokenValue { private consumeQuote(): LexerTokenValue {
return { return {
value: this.reader.consume(), value: this.reader.consume(),