static RegexExpression _ParseModifier(RegexExpression expr, ParseContext pc) { var line = pc.Line; var column = pc.Column; var position = pc.Position; switch (pc.Current) { case '*': expr = new RegexRepeatExpression(expr); expr.SetLocation(line, column, position); pc.Advance(); break; case '+': expr = new RegexRepeatExpression(expr, 1); expr.SetLocation(line, column, position); pc.Advance(); break; case '?': expr = new RegexOptionalExpression(expr); expr.SetLocation(line, column, position); pc.Advance(); break; case '{': pc.Advance(); pc.TrySkipWhiteSpace(); pc.Expecting('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '}'); var min = -1; var max = -1; if (',' != pc.Current && '}' != pc.Current) { var l = pc.CaptureBuffer.Length; pc.TryReadDigits(); min = int.Parse(pc.GetCapture(l)); pc.TrySkipWhiteSpace(); } if (',' == pc.Current) { pc.Advance(); pc.TrySkipWhiteSpace(); pc.Expecting('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '}'); if ('}' != pc.Current) { var l = pc.CaptureBuffer.Length; pc.TryReadDigits(); max = int.Parse(pc.GetCapture(l)); pc.TrySkipWhiteSpace(); } } pc.Expecting('}'); pc.Advance(); expr = new RegexRepeatExpression(expr, min, max); expr.SetLocation(line, column, position); break; } return(expr); }
private void RenderTimer_Tick(object sender, EventArgs e) { RenderTimer.Enabled = false; CharFA <string> fa = null; try { var ast = RegexExpression.Parse(Regex.Text); fa = ast.ToFA("Accept"); } catch (Exception ex) { Status.Text = string.Format("Error Parsing Regex: {0}", ex.Message); } if (null != fa) { // mark our states for displaying the DFA later foreach (var ffa in fa.FillClosure()) { ffa.Tag = ffa; } try { var kvp = fa.Lex(Input.Text.GetEnumerator(), "#ERROR"); if (kvp.Value.Length != Input.Text.Length) { Status.Text = string.Format("Input error at {0}", kvp.Value.Length); } else { Status.Text = "Successfully Lexed."; } } catch (Exception eex) { Status.Text = string.Format("Input error: {0}", eex.Message); } var options = new CharFA <string> .DotGraphOptions(); options.DebugString = Input.Text; var dfa = fa.ToDfa(); dfa.TrimDuplicates(); try { using (var stm = fa.RenderToStream("jpg", false, options)) NfaGraph.Image = Image.FromStream(stm); } catch { } options.StatePrefix = @"Q"; options.DebugSourceNfa = fa; try { using (var stm = dfa.RenderToStream("jpg", false, options)) DfaGraph.Image = Image.FromStream(stm); } catch { } } }
public RegexOrExpression(RegexExpression left, params RegexExpression[] right) { Left = left; for (int i = 0; i < right.Length; i++) { var r = right[i]; if (null == Right) { Right = r; } else { var c = new RegexOrExpression(); c.Left = Left; c.Right = Right; Right = null; Left = c; } } }
public override string ToString() { return(string.Concat(RegexExpression.EscapeRangeChar(First), "-", RegexExpression.EscapeRangeChar(Last))); }
public override string ToString() { return(RegexExpression.EscapeRangeChar(Value)); }
public LexRule(string left, RegexExpression right) { Left = left; Right = right; }
public LexRule(string left, string right) { Left = left; Right = RegexExpression.Parse(right); }
public RegexOptionalExpression(RegexExpression expression) { Expression = expression; }
internal static RegexExpression Parse(ParseContext pc) { RegexExpression result = null, next = null; int ich; pc.EnsureStarted(); var line = pc.Line; var column = pc.Column; var position = pc.Position; while (true) { switch (pc.Current) { case -1: return(result); case '.': var nset = new RegexCharsetExpression(new RegexCharsetEntry[] { new RegexCharsetRangeEntry(char.MinValue, char.MaxValue) }, false); nset.SetLocation(line, column, position); if (null == result) { result = nset; } else { result = new RegexConcatExpression(result, nset); result.SetLocation(line, column, position); } pc.Advance(); result = _ParseModifier(result, pc); line = pc.Line; column = pc.Column; position = pc.Position; break; case '\\': if (-1 != (ich = _ParseEscape(pc))) { next = new RegexLiteralExpression((char)ich); next.SetLocation(line, column, position); next = _ParseModifier(next, pc); if (null != result) { result = new RegexConcatExpression(result, next); result.SetLocation(line, column, position); } else { result = next; } } else { pc.Expecting(); // throw an error return(null); // doesn't execute } line = pc.Line; column = pc.Column; position = pc.Position; break; case ')': return(result); case '(': pc.Advance(); pc.Expecting(); next = Parse(pc); pc.Expecting(')'); pc.Advance(); next = _ParseModifier(next, pc); if (null == result) { result = next; } else { result = new RegexConcatExpression(result, next); result.SetLocation(line, column, position); } line = pc.Line; column = pc.Column; position = pc.Position; break; case '|': if (-1 != pc.Advance()) { next = Parse(pc); result = new RegexOrExpression(result, next); result.SetLocation(line, column, position); } else { result = new RegexOrExpression(result, null); result.SetLocation(line, column, position); } line = pc.Line; column = pc.Column; position = pc.Position; break; case '[': pc.ClearCapture(); pc.Advance(); pc.Expecting(); bool not = false; if ('^' == pc.Current) { not = true; pc.Advance(); pc.Expecting(); } var ranges = _ParseRanges(pc); if (ranges.Count == 0) { System.Diagnostics.Debugger.Break(); } pc.Expecting(']'); pc.Advance(); next = new RegexCharsetExpression(ranges, not); next.SetLocation(line, column, position); next = _ParseModifier(next, pc); if (null == result) { result = next; } else { result = new RegexConcatExpression(result, next); result.SetLocation(pc.Line, pc.Column, pc.Position); } line = pc.Line; column = pc.Column; position = pc.Position; break; default: ich = pc.Current; next = new RegexLiteralExpression((char)ich); next.SetLocation(line, column, position); pc.Advance(); next = _ParseModifier(next, pc); if (null == result) { result = next; } else { result = new RegexConcatExpression(result, next); result.SetLocation(line, column, position); } line = pc.Line; column = pc.Column; position = pc.Position; break; } } }
public RegexRepeatExpression(RegexExpression expression, int minOccurs = -1, int maxOccurs = -1) { Expression = expression; MinOccurs = minOccurs; MaxOccurs = maxOccurs; }
internal static LexDocument Parse(ParseContext pc) { var result = new LexDocument(); while (-1 != pc.Current) { var line = pc.Line; var column = pc.Column; var position = pc.Position; LexNode.SkipCommentsAndWhitespace(pc); while ('\n' == pc.Current) { pc.Advance(); LexNode.SkipCommentsAndWhitespace(pc); } var id = LexNode.ParseIdentifier(pc); if (string.IsNullOrEmpty(id)) { pc.Advance(); LexNode.SkipCommentsAndWhitespace(pc); continue; } LexNode.SkipCommentsAndWhitespace(pc); pc.Expecting(':', '-', '='); if (':' == pc.Current) // attribute set { pc.Advance(); var d = new LexAttributeList(); while (-1 != pc.Current && '\n' != pc.Current) { var attr = LexAttribute.Parse(pc); d.Add(attr); LexNode.SkipCommentsAndWhitespace(pc); pc.Expecting('\n', ',', -1); if (',' == pc.Current) { pc.Advance(); } } result.AttributeSets.Add(id, d); LexNode.SkipCommentsAndWhitespace(pc); } else if ('=' == pc.Current) { pc.Advance(); LexNode.SkipCommentsAndWhitespace(pc); pc.Expecting('\''); pc.Advance(); var l = pc.CaptureBuffer.Length; pc.TryReadUntil('\'', '\\', false); pc.Expecting('\''); pc.Advance(); var rx = pc.GetCapture(l); // make sure to capture the line numbers properly: var rpc = ParseContext.Create(rx); rpc.EnsureStarted(); rpc.SetLocation(pc.Line, pc.Column, pc.Position); var rule = new LexRule(id, RegexExpression.Parse(rpc)); rule.SetLocation(line, column, position); result.Rules.Add(rule); } else if ('-' == pc.Current) { pc.TrySkipUntil('\n', true); } LexNode.SkipCommentsAndWhitespace(pc); if ('\n' == pc.Current) { pc.Advance(); } } return(result); }