Skip to content

Commit

Permalink
refactor: replace isRegExp()
Browse files Browse the repository at this point in the history
  • Loading branch information
bd82 committed Jul 2, 2023
1 parent 01aee95 commit 3986b63
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/chevrotain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"@chevrotain/utils": "10.5.0",
"@chevrotain/regexp-to-ast": "10.5.0",
"lodash": "4.17.21",
"remeda": "1.23.0"
"remeda": "1.23.0",
"is-regexp": "2.1.0"
},
"devDependencies": {
"@types/lodash": "4.14.191",
Expand Down
2 changes: 1 addition & 1 deletion packages/chevrotain/src/parse/cst/cst_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import map from "lodash/map"
import forEach from "lodash/forEach"
import filter from "lodash/filter"
import keys from "lodash/keys"
import isFunction from "lodash/isFunction"
import { isFunction } from "remeda"
import { defineNameProp } from "../../lang/lang_extensions"
import { CstNode, ICstVisitor } from "@chevrotain/types"

Expand Down
6 changes: 4 additions & 2 deletions packages/chevrotain/src/parse/parser/traits/gast_recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import peek from "lodash/last"
import { isArray } from "remeda"
import some from "lodash/some"
import forEach from "lodash/forEach"
import isFunction from "lodash/isFunction"
import { isFunction } from "remeda"
import has from "lodash/has"
import { MixedInParser } from "./parser_traits"
import {
Expand Down Expand Up @@ -370,7 +370,9 @@ function recordProd(
): any {
assertMethodIdxIsValid(occurrence)
const prevProd: any = peek(this.recordingProdStack)
const grammarAction = isFunction(mainProdArg) ? mainProdArg : mainProdArg.DEF
const grammarAction = isFunction(mainProdArg)
? mainProdArg
: (mainProdArg as DSLMethodOpts<any>).DEF

const newProd = new prodConstructor({ definition: [], idx: occurrence })
if (handleSep) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { isArray } from "remeda"
import flatten from "lodash/flatten"
import every from "lodash/every"
import uniq from "lodash/uniq"
import isObject from "lodash/isObject"
import { isObject } from "remeda"
import has from "lodash/has"
import values from "lodash/values"
import reduce from "lodash/reduce"
Expand Down
24 changes: 12 additions & 12 deletions packages/chevrotain/src/scan/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import first from "lodash/first"
import { isEmpty } from "remeda"
import { compact } from "remeda"
import { isArray } from "remeda"
import isRegexp from "is-regexp"
import values from "lodash/values"
import flatten from "lodash/flatten"
import reject from "lodash/reject"
import difference from "lodash/difference"
import indexOf from "lodash/indexOf"
import map from "lodash/map"
import forEach from "lodash/forEach"
import isString from "lodash/isString"
import isFunction from "lodash/isFunction"
import { isString } from "remeda"
import { isFunction } from "remeda"
import find from "lodash/find"
import has from "lodash/has"
import keys from "lodash/keys"
import isRegExp from "lodash/isRegExp"
import filter from "lodash/filter"
import defaults from "lodash/defaults"
import reduce from "lodash/reduce"
Expand Down Expand Up @@ -116,7 +116,7 @@ export function analyzeTokenTypes(
const currPattern = currType[PATTERN]

/* istanbul ignore else */
if (isRegExp(currPattern)) {
if (isRegexp(currPattern)) {
const regExpSource = currPattern.source
if (
regExpSource.length === 1 &&
Expand Down Expand Up @@ -332,7 +332,7 @@ export function analyzeTokenTypes(
)
}
})
} else if (isRegExp(currTokType.PATTERN)) {
} else if (isRegexp(currTokType.PATTERN)) {
if (currTokType.PATTERN.unicode) {
canBeOptimized = false
if (options.ensureOptimizations) {
Expand Down Expand Up @@ -421,7 +421,7 @@ function validateRegExpPattern(
): ILexerDefinitionError[] {
let errors: ILexerDefinitionError[] = []
const withRegExpPatterns = filter(tokenTypes, (currTokType) =>
isRegExp(currTokType[PATTERN])
isRegexp(currTokType[PATTERN])
)

errors = errors.concat(findEndOfInputAnchor(withRegExpPatterns))
Expand Down Expand Up @@ -470,7 +470,7 @@ export function findInvalidPatterns(
const tokenTypesWithInvalidPattern = filter(tokenTypes, (currType) => {
const pattern = currType[PATTERN]
return (
!isRegExp(pattern) &&
!isRegexp(pattern) &&
!isFunction(pattern) &&
!has(pattern, "exec") &&
!isString(pattern)
Expand Down Expand Up @@ -748,7 +748,7 @@ export function findUnreachablePatterns(
// deeper regExp analysis capabilities
if (isString(pattern)) {
result.push({ str: pattern, idx, tokenType: tokType })
} else if (isRegExp(pattern) && noMetaChar(pattern)) {
} else if (isRegexp(pattern) && noMetaChar(pattern)) {
result.push({ str: pattern.source, idx, tokenType: tokType })
}
return result
Expand Down Expand Up @@ -778,15 +778,15 @@ export function findUnreachablePatterns(

function testTokenType(str: string, pattern: any): boolean {
/* istanbul ignore else */
if (isRegExp(pattern)) {
if (isRegexp(pattern)) {
const regExpArray = pattern.exec(str)
return regExpArray !== null && regExpArray.index === 0
} else if (isFunction(pattern)) {
// maintain the API of custom patterns
return pattern(str, 0, [], {})
} else if (has(pattern, "exec")) {
// maintain the API of custom patterns
return pattern.exec(str, 0, [], {})
return (pattern as any).exec(str, 0, [], {})
} else if (typeof pattern === "string") {
return pattern === str
} else {
Expand Down Expand Up @@ -983,7 +983,7 @@ export function cloneEmptyGroups(emptyGroups: {
export function isCustomPattern(tokenType: TokenType): boolean {
const pattern = tokenType.PATTERN
/* istanbul ignore else */
if (isRegExp(pattern)) {
if (isRegexp(pattern)) {
return false
} else if (isFunction(pattern)) {
// CustomPatternMatcherFunc - custom patterns do not require any transformations, only wrapping in a RegExp Like object
Expand Down Expand Up @@ -1050,7 +1050,7 @@ function checkLineBreaksIssues(
return false
} else {
/* istanbul ignore else */
if (isRegExp(tokType.PATTERN)) {
if (isRegexp(tokType.PATTERN)) {
try {
// TODO: why is the casting suddenly needed?
canMatchCharCode(lineTerminatorCharCodes, tokType.PATTERN as RegExp)
Expand Down
2 changes: 1 addition & 1 deletion packages/chevrotain/src/scan/tokens_public.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isString from "lodash/isString"
import { isString } from "remeda"
import has from "lodash/has"
import { Lexer } from "./lexer_public"
import { augmentTokenTypes, tokenStructuredMatcher } from "./tokens"
Expand Down
2 changes: 1 addition & 1 deletion packages/chevrotain/test/full_flow/parse_tree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { compact } from "remeda"
import isFunction from "lodash/isFunction"
import { isFunction } from "remeda"
import { IToken, TokenType } from "@chevrotain/types"

export class ParseTree {
Expand Down
8 changes: 4 additions & 4 deletions packages/chevrotain/test/scan/lexer_spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import last from "lodash/last"
import map from "lodash/map"
import forEach from "lodash/forEach"
import isString from "lodash/isString"
import isRegExp from "lodash/isRegExp"
import { isString } from "remeda"
import isRegexp from "is-regexp"
import keys from "lodash/keys"
import { createToken } from "../../src/scan/tokens_public"
import { Lexer, LexerDefinitionErrorType } from "../../src/scan/lexer_public"
Expand Down Expand Up @@ -737,7 +737,7 @@ function defineLexerSpecs(
])

forEach(allPatterns, (currPattern) => {
if (isRegExp(currPattern)) {
if (isRegexp(currPattern)) {
expect(currPattern.sticky).to.be.true
}
})
Expand Down Expand Up @@ -2218,7 +2218,7 @@ function wrapWithCustom(baseExtendToken: (c: ITokenConfig) => TokenType) {

const pattern = newToken.PATTERN
if (
isRegExp(pattern) &&
isRegexp(pattern) &&
!/\\n|\\r|\\s/g.test(pattern.source) &&
pattern !== Lexer.NA
) {
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3986b63

Please sign in to comment.