// Algo réalisé à l'aide de la grammaire formelle public static NodeAST MatchRegex(StringCursor stringCursor) { NodeAST node = MatchAlternative(stringCursor); if (node.Type == NodeASTType.Error) { return(node); } return(stringCursor.Match('\0') ? node : NodeAST.CreateError()); }
public static NodeAST MatchConstant(StringCursor stringCursor) { if (!char.IsLetterOrDigit(stringCursor.Current)) { return(NodeAST.CreateError()); } NodeAST node = new NodeAST(NodeASTType.Constant, stringCursor.Current); stringCursor.Next(); return(node); }
//Expression starable qui contient, ou pas, une étoile public static NodeAST MatchStar(StringCursor stringCursor) { NodeAST node = MatchStarable(stringCursor); if (node.Type == NodeASTType.Error) { return(node); } if (stringCursor.Match('*')) { stringCursor.Next(); return(new NodeAST(NodeASTType.Star, new NodeAST[] { node })); } return(node); }
public static NodeAST MatchConcatenation(StringCursor stringCursor) { List <NodeAST> children = new List <NodeAST>(); do { NodeAST node = MatchStar(stringCursor); if (node.Type == NodeASTType.Error) { return(node); } children.Add(node); } while (stringCursor.Match('(') || char.IsLetterOrDigit(stringCursor.Current)); return((children.Count == 1) ? children[0] : new NodeAST(NodeASTType.Concatenation, children.ToArray())); }
public static NodeAST MatchParenthesis(StringCursor stringCursor) { if (!stringCursor.Match('(')) { return(NodeAST.CreateError()); } stringCursor.Next(); NodeAST node = MatchAlternative(stringCursor); if (node.Type == NodeASTType.Error) { return(node); } if (!stringCursor.Match(')')) { return(NodeAST.CreateError()); } stringCursor.Next(); return(node); }
public static NodeAST MatchAlternative(StringCursor stringCursor) { NodeAST node = MatchConcatenation(stringCursor); if (node.Type == NodeASTType.Error) { return(node); } List <NodeAST> children = new List <NodeAST>(); children.Add(node); while (stringCursor.Match('|')) { stringCursor.Next(); node = MatchConcatenation(stringCursor); if (node.Type == NodeASTType.Error) { return(node); } children.Add(node); } return((children.Count == 1) ? children[0] : new NodeAST(NodeASTType.Alternative, children.ToArray())); }