public void TestRegExLiteral() { Parser parser; Token token; var term = new RegexLiteral("RegEx"); parser = TestHelper.CreateParser(term); token = parser.ParseInput(@"/abc\\\/de/gm "); Assert.IsNotNull(token, "Failed to produce a token on valid string."); Assert.AreEqual(term, token.Terminal, "Failed to scan a string - invalid Terminal in the returned token."); Assert.IsNotNull(token.Value, "Token Value field is null - should be Regex object."); var regex = token.Value as Regex; Assert.IsNotNull(regex, "Failed to create Regex object."); var match = regex.Match(@"00abc\/de00"); Assert.AreEqual(match.Index, 2, "Failed to match a regular expression"); }
public void TestRegExLiteral() { var term = new RegexLiteral("RegEx"); var parser = TestHelper.CreateParser(term); var token = parser.ParseInput(@"/abc\\\/de/gm "); Assert.IsNotNull(token, "Failed to produce a token on valid string."); Assert.AreEqual(term, token.Terminal, "Failed to scan a string - invalid Terminal in the returned token."); Assert.IsNotNull(token.Value, "Token Value field is null - should be Regex object."); var regex = token.Value as Regex; Assert.IsNotNull(regex, "Failed to create Regex object."); var match = regex.Match(@"00abc\/de00"); Assert.AreEqual(match.Index, 2, "Failed to match a regular expression"); }
/// <summary> /// 使うな /// </summary> public Kbtter3QueryGrammar() : base() { //コメント var comment = new CommentTerminal("Comment", "/*", "*/"); NonGrammarTerminals.Add(comment); //リテラル var number = new NumberLiteral("Number", NumberOptions.AllowSign | NumberOptions.AllowStartEndDot); var str = new StringLiteral("String", "\""); var regex = new RegexLiteral("Regex", '/', '\\'); var ident = new IdentifierTerminal("Identifer"); //非終端 var Value = new NonTerminal("Value"); var Term = new NonTerminal("Term"); var Expression = new NonTerminal("Expression"); var BinExpression = new NonTerminal("BinExpression"); var ParExpression = new NonTerminal("ParExpression"); var PostfixExpression = new NonTerminal("PostfixExpression"); var Operator = new NonTerminal("Operator"); //非終端定義 Value.Rule = number | str | ident | regex | "null" | "true" | "false"; Term.Rule = Value | ParExpression; Operator.Rule = ToTerm("==") | "!=" | ">" | "<" | ">=" | "<=" | "match" | "&&" | "||" | "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "."; BinExpression.Rule = Expression + Operator + Expression; PostfixExpression.Rule = (ToTerm("+") + Term) | ("-" + Term) | ("!" + Term); Expression.Rule = BinExpression | Term | PostfixExpression; ParExpression.Rule = ToTerm("(") + Expression + ")"; RegisterOperators(10, "."); RegisterOperators(9, "*", "/", "%"); RegisterOperators(8, "+", "-"); RegisterOperators(7, ">", "<", ">=", "<=", "match"); RegisterOperators(6, "==", "!="); RegisterOperators(5, "&"); RegisterOperators(4, "^"); RegisterOperators(3, "|"); RegisterOperators(2, "&&"); RegisterOperators(1, "||"); Root = Expression; MarkPunctuation("(", ")"); MarkTransient(Expression, ParExpression, Value, Operator, Term); }
public Kbtter3QueryGrammar() : base(false) { //コメント var comment = new CommentTerminal("Comment", "/*", "*/"); NonGrammarTerminals.Add(comment); //リテラル var number = new NumberLiteral("Number", NumberOptions.AllowSign | NumberOptions.AllowStartEndDot); var str = new StringLiteral("String", "\""); var regex = new RegexLiteral("Regex", '/', '\\'); var ident = new IdentifierTerminal("Identifer"); //非終端 var Value = new NonTerminal("Value"); var Term = new NonTerminal("Term"); var Expression = new NonTerminal("Expression"); var BinExpression = new NonTerminal("BinExpression"); var ParExpression = new NonTerminal("ParExpression"); var PostfixExpression = new NonTerminal("PostfixExpression"); var Operator = new NonTerminal("Operator"); //非終端定義 Value.Rule = number | str | ident | regex | "null" | "true" | "false"; Term.Rule = Value | ParExpression; Operator.Rule = ToTerm("==") | "!=" | ">" | "<" | ">=" | "<=" | "match" | "&&" | "||" | "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "."; BinExpression.Rule = Expression + Operator + Expression; PostfixExpression.Rule = (ToTerm("+") + Term) | ("-" + Term) | ("!" + Term); Expression.Rule = BinExpression | Term | PostfixExpression; ParExpression.Rule = ToTerm("(") + Expression + ")"; RegisterOperators(10, "."); RegisterOperators(9, "*", "/", "%"); RegisterOperators(8, "+", "-"); RegisterOperators(7, ">", "<", ">=", "<=", "match"); RegisterOperators(6, "==", "!="); RegisterOperators(5, "&"); RegisterOperators(4, "^"); RegisterOperators(3, "|"); RegisterOperators(2, "&&"); RegisterOperators(1, "||"); Root = Expression; MarkPunctuation("(", ")"); MarkTransient(Expression, ParExpression, Value, Operator, Term); }
public void TestRegExLiteral() { Parser parser; Token token; var term = new RegexLiteral("RegEx"); parser = TestHelper.CreateParser(term); token = parser.ParseInput(@"/abc\\\/de/gm "); Assert.False(token == null, "Failed to produce a token on valid string."); Assert.True(term == token.Terminal, "Failed to scan a string - invalid Terminal in the returned token."); Assert.False(token.Value == null, "Token Value field is null - should be Regex object."); var regex = token.Value as Regex; Assert.False(regex == null, "Failed to create Regex object."); var match = regex.Match(@"00abc\/de00"); Assert.True(match.Index == 2, "Failed to match a regular expression"); }
public static bool IsLiteral(char c) => RegexLiteral.IsMatch(c + "");
/// <summary> /// Visit operation called for regex literals. /// /// <param name="lit">a regex literal</param> /// </summary> public virtual void visit_regex_literal(RegexLiteral lit) { }