Skip to content

Commit 1cfae0c

Browse files
committed
Add ParseOption to regex parser for case-insensitive mode
1 parent d90ac9e commit 1cfae0c

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

regex/Regex/Regex/Basic.lean

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,24 @@ def fromExpr (expr : Expr) : Regex :=
8181
{ nfa := nfa, wf := Regex.NFA.compile_wf, optimizationInfo := optimizationInfo }
8282

8383
/--
84-
Parses a regular expression string into a `Regex` structure.
85-
84+
Parses a regular expression string into a `Regex` structure with options.
8685
* `s`: The regular expression string to parse
86+
* `options`: Parsing options
8787
* Returns: Either a compiled `Regex` or a parsing error
8888
-/
89-
def parse (s : String) : Except Regex.Syntax.Parser.Error Regex := do
90-
let expr ← Regex.Syntax.Parser.parse s
89+
def parseAux (s : String) (options : Regex.Syntax.Parser.ParseOption) : Except Regex.Syntax.Parser.Error Regex := do
90+
let expr ← Regex.Syntax.Parser.parseAux s options
9191
return Regex.fromExpr expr
9292

93+
/--
94+
Parses a regular expression string into a `Regex` structure with default options.
95+
96+
* `s`: The regular expression string to parse
97+
* Returns: Either a compiled `Regex` or a parsing error
98+
-/
99+
def parse (s : String) : Except Regex.Syntax.Parser.Error Regex :=
100+
parseAux s {}
101+
93102
/--
94103
Parses a regular expression string into a `Regex` structure, panicking on parse error.
95104

regex/Regex/Syntax/Parser/Basic.lean

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,19 @@ termination_by (pos, 100)
333333

334334
end
335335

336+
structure ParseOption where
337+
caseInsensitive : Bool := false
338+
336339
def parseAst (input : String) : Except Error Ast :=
337340
regex input.startPos
338341
|>.complete .expectedEof
339342
|>.toExcept
340343

341-
def parse (input : String) : Except Error Expr :=
344+
def parseAux (input : String) (options : ParseOption) : Except Error Expr :=
342345
parseAst input
343-
|>.map fun ast => Ast.toRegex (.group ast)
346+
|>.map fun ast => Ast.toRegexAux (ToRegexState.mk 0 options.caseInsensitive) (.group ast) |>.2
347+
348+
def parse (input : String) : Except Error Expr := parseAux input {}
344349

345350
def parse! (input : String) : Expr :=
346351
match parse input with

0 commit comments

Comments
 (0)