1
0
mirror of https://github.com/pdemian/human2regex.git synced 2025-05-16 20:40:08 -07:00
This commit is contained in:
Patrick Demian 2020-10-10 05:02:26 -04:00
parent 40ca670a2a
commit 1706461be7
5 changed files with 1270 additions and 71 deletions

99
.eslintrc.json Normal file
View File

@ -0,0 +1,99 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-inferrable-types": "off",
"curly": "warn",
"no-loss-of-precision": "error",
"default-case-last": "warn",
"default-case": "error",
"dot-location": [
"warn",
"property"
],
"dot-notation": "error",
"no-alert": "error",
"no-empty-function": "error",
"no-eval": "error",
"no-useless-escape": "off",
"no-implicit-coercion": "error",
"no-implied-eval": "error",
"no-invalid-this": "error",
"no-new": "error",
"no-unmodified-loop-condition": "error",
"init-declarations": [
"error",
"always"
],
"no-shadow": "error",
"no-undefined": "error",
"brace-style": [
"error",
"stroustrup"
],
"comma-spacing": [
"error",
{ "before": false, "after": true }
],
"array-bracket-spacing": [
"error",
"always"
],
"comma-style": [
"error",
"last"
],
"func-style": [
"error",
"declaration"
],
"no-bitwise": "error",
"no-lonely-if": "error",
"no-multi-assign": "error",
"semi-style": [
"error",
"last"
],
"arrow-parens": [
"error",
"always"
],
"no-var": "error",
"prefer-const": "error",
"eqeqeq": [
"error",
"always"
],
/*"indent": [
"error",
4
],*/
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"warn",
"double",
{ "avoidEscape": true }
],
"semi": [
"error",
"always"
]
}
}

1093
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,10 +11,14 @@
"@types/jquery": "^3.5.2", "@types/jquery": "^3.5.2",
"@types/mustache": "^4.0.1", "@types/mustache": "^4.0.1",
"@types/uglify-es": "^3.0.0", "@types/uglify-es": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"eslint": "^7.11.0",
"fs-extra": "^9.0.0", "fs-extra": "^9.0.0",
"glob": "^7.1.6", "glob": "^7.1.6",
"html-minifier": "^4.0.0", "html-minifier": "^4.0.0",
"mustache": "^4.0.1", "mustache": "^4.0.1",
"typescript": "^4.0.3",
"uglify-es": "^3.3.9" "uglify-es": "^3.3.9"
}, },
"scripts": { "scripts": {

View File

@ -6,6 +6,6 @@ export class ParserOptions {
} }
export function parse(tokens: Token[]) { export function parse(tokens: Token[]) : null {
return undefined; return null;
} }

View File

@ -56,16 +56,16 @@ const keywords = {
}; };
const escape_sequences = { const escape_sequences = {
'a': '\a', "a": "\a",
'b': '\b', "b": "\b",
'e': '\e', "e": "\e",
'f': '\f', "f": "\f",
'n': '\n', "n": "\n",
'r': '\r', "r": "\r",
't': '\t', "t": "\t",
'"': '"', "'": "'",
'\'': '\'', "\"": '"',
'\\': '\\', "\\": "\\",
}; };
export class TokenizerOptions { export class TokenizerOptions {
@ -77,35 +77,37 @@ const escape_sequence_hex_regex = new RegExp(/[0-9A-Fa-f]/g);
function escape_sequence_gather_hex(input: string, i : number, max: number) : string { function escape_sequence_gather_hex(input: string, i : number, max: number) : string {
let hex = ""; let hex = "";
for(i++; i < input.length && max-- > 0; i++) { for(i++; i < input.length && max-- > 0; i++) {
if(escape_sequence_hex_regex.test(input[i])) hex += input[i]; if(escape_sequence_hex_regex.test(input[i])) {
hex += input[i];
}
} }
return hex; return hex;
} }
function escape_sequence_mapper(input: string, i : number) : { code: string, read: number, error?: Error } { function escape_sequence_mapper(input: string, i : number) : { code: string, read: number, error?: Error } {
if(escape_sequences[input[i]] != undefined) { if(escape_sequences[input[i]]) {
return { code: escape_sequences[input[i]], read: 1 }; return { code: escape_sequences[input[i]], read: 1 };
} }
//variable hex code //variable hex code
else if(input[i] == 'x') { else if(input[i] === "x") {
const hex = escape_sequence_gather_hex(input, ++i, 4); const hex = escape_sequence_gather_hex(input, ++i, 4);
return { code: String.fromCharCode(parseInt(hex, 16)), read: hex.length + 1 }; return { code: String.fromCharCode(parseInt(hex, 16)), read: hex.length + 1 };
} }
//4 hex unicode //4 hex unicode
else if(input[i] == 'u') { else if(input[i] === "u") {
const unicode = escape_sequence_gather_hex(input, ++i, 4); const unicode = escape_sequence_gather_hex(input, ++i, 4);
if(unicode.length != 4) { if(unicode.length !== 4) {
return { code: "", read: unicode.length + 1, error: new Error("Bad escape sequence")}; return { code: "", read: unicode.length + 1, error: new Error("Bad escape sequence")};
} }
else { else {
return { code: String.fromCharCode(parseInt(unicode, 16)), read: 5 }; return { code: String.fromCharCode(parseInt(unicode, 16)), read: 5 };
} }
} }
else if(input[i] == 'U') { else if(input[i] === "U") {
const unicode = escape_sequence_gather_hex(input, ++i, 8); const unicode = escape_sequence_gather_hex(input, ++i, 8);
if(unicode.length != 8) { if(unicode.length !== 8) {
return { code: "", read: unicode.length + 1, error: new Error("Bad escape sequence")}; return { code: "", read: unicode.length + 1, error: new Error("Bad escape sequence")};
} }
else { else {
@ -137,8 +139,8 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
let line = 1; let line = 1;
let position = 1; let position = 1;
let tokens : Token[] = []; const tokens : Token[] = [];
let errors : TokenError[] = []; const errors : TokenError[] = [];
for(let i = 0; i < input.length; i++, position++) { for(let i = 0; i < input.length; i++, position++) {
// 4 spaces = 1 tab. That is final. Debate over // 4 spaces = 1 tab. That is final. Debate over
@ -161,7 +163,7 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
// comments // comments
else if(input.startsWith("//", i)) { else if(input.startsWith("//", i)) {
for(i++, position++; i < input.length; i++, position++) { for(i++, position++; i < input.length; i++, position++) {
if(input[i] == '\n') { if(input[i] === "\n") {
tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position)); tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position));
break; break;
} }
@ -171,18 +173,18 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
} }
else if(input.startsWith("/*", i)) { else if(input.startsWith("/*", i)) {
for(i++, position++; i < input.length-1; i++, position++) { for(i++, position++; i < input.length-1; i++, position++) {
if(input[i] == '*' && input[i+1] == '/') { if(input[i] === "*" && input[i+1] === "/") {
tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position)); tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position));
i++; i++;
position++; position++;
break; break;
} }
if(input[i] == '\n') { if(input[i] === "\n") {
line++; line++;
position = 0; position = 0;
} }
} }
if(i == input.length-1) { if(i === input.length-1) {
errors.push(new TokenError("Unexpected EOF", line, position)); errors.push(new TokenError("Unexpected EOF", line, position));
} }
else { else {
@ -199,9 +201,9 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
else { else {
switch(input[i]) { switch(input[i]) {
// comment // comment
case '#': case "#":
for(i++, position++; i < input.length; i++, position++) { for(i++, position++; i < input.length; i++, position++) {
if(input[i] == '\n') { if(input[i] === "\n") {
tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position)); tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position));
line++; line++;
position = 0; position = 0;
@ -212,6 +214,7 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
// quote // quote
case '"': case '"':
case '\"': case '\"':
{
// build up a word between quotes // build up a word between quotes
const quote_begin = { line: line, position: position }; const quote_begin = { line: line, position: position };
const quote_char = input[i]; const quote_char = input[i];
@ -222,12 +225,12 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
do { do {
i++; i++;
position++; position++;
if(input[i] == '\\') { if(input[i] === "\\") {
i++; i++;
position++; position++;
const sequence = escape_sequence_mapper(input, i); const sequence = escape_sequence_mapper(input, i);
if(sequence.error != undefined) { if(sequence.error) {
errors.push(new TokenError(sequence.error.message, line, position)); errors.push(new TokenError(sequence.error.message, line, position));
} }
@ -236,11 +239,11 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
quote += sequence.code; quote += sequence.code;
} }
else if(input[i] == quote_char) { else if(input[i] === quote_char) {
found_ending = true; found_ending = true;
break; break;
} }
else if(input[i] == '\n') { else if(input[i] === "\n") {
line++; line++;
position = 0; position = 0;
break; break;
@ -260,21 +263,21 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
position = 0; position = 0;
} }
break; break;
}
// between (ex: 0...3 or 0-3) // between (ex: 0...3 or 0-3)
case '-': case "-":
tokens.push(new Token(TokenType.BETWEEN, line, position)); tokens.push(new Token(TokenType.BETWEEN, line, position));
break; break;
case '\n': case "\n":
tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position)); tokens.push(new Token(TokenType.END_OF_STATEMENT, line, position));
break; break;
case '\r': case "\r":
// ignore // ignore
break; break;
case '\t': case "\t":
tokens.push(new Token(TokenType.INDENT, line, position)); tokens.push(new Token(TokenType.INDENT, line, position));
break; break;
case ' ': case " ":
break; break;
default: default:
// is digit? build up a number // is digit? build up a number
@ -299,7 +302,7 @@ export function tokenize(input: string, options: TokenizerOptions) : { tokens: T
const keyword_text = text.toLowerCase(); const keyword_text = text.toLowerCase();
if(keywords[keyword_text] != undefined) { if(keywords[keyword_text]) {
tokens.push(new Token(keywords[keyword_text], line, position)); tokens.push(new Token(keywords[keyword_text], line, position));
} }
else { else {