From cf2f200a16203311a3ac7595eb547b37fab5df82 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Tue, 4 Dec 2018 13:24:49 -0600 Subject: Fix performance issues with regex --- nimterop/ast.nim | 5 +---- nimterop/globals.nim | 3 +++ nimterop/grammar.nim | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/nimterop/ast.nim b/nimterop/ast.nim index ba7561e..df973b9 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -53,10 +53,7 @@ proc searchAstForNode(ast: ref Ast, node: TSNode): bool = return if ast.children.len != 0: - let - rstr = ast.getRegexForAstChildren() - - if childNames.contains(rstr.toPattern): + if childNames.contains(ast.regex): if node.getTSNodeNamedChildCountSansComments() != 0: var flag = true for i in 0 .. node.tsNodeNamedChildCount()-1: diff --git a/nimterop/globals.nim b/nimterop/globals.nim index 39969bc..e65a4b9 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -1,5 +1,7 @@ import tables +import regex + type Kind* = enum exactlyOne @@ -12,6 +14,7 @@ type kind*: Kind children*: seq[ref Ast] tonim*: proc () {.closure, locks: 0.} + regex*: Regex State* = object compile*, defines*, headers*, includeDirs*, searchDirs*: seq[string] diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index a07ad84..7876ca3 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -1,5 +1,7 @@ import strformat, tables +import regex + import "."/[getters, globals, lisp] proc initGrammar() = @@ -254,6 +256,13 @@ proc initGrammar() = )) +proc initRegex(ast: ref Ast) = + if ast.children.len != 0: + for child in ast.children: + child.initRegex() + + ast.regex = ast.getRegexForAstChildren().re() + proc parseGrammar*() = initGrammar() @@ -263,6 +272,7 @@ proc parseGrammar*() = ast = gStateRT.grammar[i].grammar.parseLisp() ast.tonim = gStateRT.grammar[i].call + ast.initRegex() if ast.name notin gStateRT.ast: gStateRT.ast[ast.name] = @[ast] else: -- cgit v1.2.3