-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
OlgaLarina
committed
Dec 26, 2023
1 parent
1d6b06c
commit 0b0e33f
Showing
10 changed files
with
274 additions
and
164 deletions.
There are no files selected for viewing
This file contains 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,71 @@ | ||
import { settings } from "./mask_utils"; | ||
|
||
export type LexemTokenType = "literal" | "expression" | "or"; | ||
export interface ILexemToken { | ||
data: any; | ||
type: LexemTokenType; | ||
isConst?: boolean; | ||
quantifier?: "+" | "*"; | ||
} | ||
|
||
export class LexicalAnalyzer { | ||
getLexems(str: string): Array<ILexemToken> { | ||
const result: Array<ILexemToken> = []; | ||
|
||
let prevChartIsEscaped = false; | ||
let prevTocken: ILexemToken; | ||
let currentTocken: ILexemToken; | ||
let bracketCounter = 0; | ||
let subString = ""; | ||
|
||
for(let index = 0; index < str.length; index++) { | ||
const currentChar = str[index]; | ||
currentTocken = null; | ||
|
||
if(bracketCounter !== 0) { | ||
if(currentChar === ")") { | ||
bracketCounter--; | ||
if(bracketCounter === 0) { | ||
currentTocken = <ILexemToken>{ data: subString, type: "expression" }; | ||
subString = ""; | ||
} else { | ||
subString += currentChar; | ||
} | ||
} else { | ||
subString += currentChar; | ||
} | ||
} else { | ||
if(prevChartIsEscaped) { | ||
prevChartIsEscaped = false; | ||
currentTocken = <ILexemToken>{ data: currentChar, isConst: true }; | ||
} else { | ||
switch (currentChar) { | ||
case settings.escapedChar: | ||
prevChartIsEscaped = true; | ||
break; | ||
case "|": | ||
currentTocken = <ILexemToken>{ data: currentChar, type: "or" }; | ||
break; | ||
case "(": | ||
bracketCounter++; | ||
break; | ||
case "*": | ||
prevTocken.quantifier = "*"; | ||
break; | ||
case "+": | ||
prevTocken.quantifier = "+"; | ||
break; | ||
default: | ||
currentTocken = <ILexemToken>{ data: currentChar, type: "literal" }; | ||
} | ||
} | ||
} | ||
|
||
if(!!currentTocken) { | ||
result.push(currentTocken); | ||
} | ||
prevTocken = currentTocken; | ||
} | ||
return result; | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.