本文整理汇总了C#中Irony.Parsing.NonTerminal.SetFlag方法的典型用法代码示例。如果您正苦于以下问题:C# NonTerminal.SetFlag方法的具体用法?C# NonTerminal.SetFlag怎么用?C# NonTerminal.SetFlag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Irony.Parsing.NonTerminal
的用法示例。
在下文中一共展示了NonTerminal.SetFlag方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SonesGQLGrammar
//.........这里部分代码省略.........
| updateEdgesStmt
| dropVertexTypeStmt
| dropEdgeTypeStmt
| dropIndexStmt
| createIndexStmt
| createTypesStmt
| createEdgeTypesStmt
| deleteStmt
| truncateStmt
| DescrInfoStmt
| insertorupdateStmt
| insertorreplaceStmt
| replaceStmt
| dumpStmt
| transactStmt
| commitRollBackTransactStmt
| rebuildIndicesStmt
| NT_ImportStmt
| linkStmt
| unlinkStmt;
#endregion
#region misc
#region ID
#region wo functions
Id_simple.Rule = name;
EdgeTraversalWithOutFunctions.Rule = dotWrapper + Id_simple;
NT_Id.SetFlag(TermFlags.IsList);
NT_Id.Rule = Id_simple
| NT_Id + EdgeTraversalWithOutFunctions;
//old
//Id.Rule = MakePlusRule(Id, dotWrapper, Id_simple);
idlist.Rule = MakePlusRule(idlist, S_comma, NT_Id);
id_simpleList.Rule = MakePlusRule(id_simpleList, S_comma, Id_simple);
id_simpleDotList.Rule = MakePlusRule(id_simpleDotList, S_dot, Id_simple);
id_typeAndAttribute.Rule = VertexTypeWrapper + S_dot + NT_Id;
#endregion
#region ID_or_Func
IdOrFunc.Rule = name
| NT_FuncCall;
dotWrapper.Rule = S_edgeTraversalDelimiter;
edgeAccessorWrapper.Rule = S_edgeInformationDelimiterSymbol;
//IDOrFuncDelimiter.Rule = dotWrapper
// | edgeAccessorWrapper;
EdgeTraversalWithFunctions.Rule = dotWrapper + IdOrFunc;
EdgeInformation.Rule = edgeAccessorWrapper + Id_simple;
IdOrFuncList.SetFlag(TermFlags.IsList);
IdOrFuncList.Rule = IdOrFunc
| IdOrFuncList + EdgeInformation
| IdOrFuncList + EdgeTraversalWithFunctions;
示例2: MakeStarRule
public static BnfExpression MakeStarRule(NonTerminal listNonTerminal, BnfTerm delimiter, BnfTerm listMember, TermListOptions options) {
bool allowTrailingDelimiter = (options & TermListOptions.AllowTrailingDelimiter) != 0;
if (delimiter == null) {
//it is much simpler case
listNonTerminal.SetFlag(TermFlags.IsList);
listNonTerminal.Rule = _currentGrammar.Empty | listNonTerminal + listMember;
return listNonTerminal.Rule;
}
//Note that deceptively simple version of the star-rule
// Elem* -> Empty | Elem | Elem* + delim + Elem
// does not work when you have delimiters. This simple version allows lists starting with delimiters -
// which is wrong. The correct formula is to first define "Elem+"-list, and then define "Elem*" list
// as "Elem* -> Empty|Elem+"
NonTerminal plusList = new NonTerminal(listMember.Name + "+");
plusList.Rule = MakePlusRule(plusList, delimiter, listMember);
plusList.SetFlag(TermFlags.NoAstNode); //to allow it to have AstNodeType not assigned
if (allowTrailingDelimiter)
listNonTerminal.Rule = _currentGrammar.Empty | plusList | plusList + delimiter;
else
listNonTerminal.Rule = _currentGrammar.Empty | plusList;
listNonTerminal.SetFlag(TermFlags.IsListContainer);
return listNonTerminal.Rule;
}
示例3: MakeListRule
protected BnfExpression MakeListRule(NonTerminal list, BnfTerm delimiter, BnfTerm listMember, TermListOptions options = TermListOptions.PlusList)
{
//If it is a star-list (allows empty), then we first build plus-list
var isPlusList = !options.IsSet(TermListOptions.AllowEmpty);
var allowTrailingDelim = options.IsSet(TermListOptions.AllowTrailingDelimiter) && delimiter != null;
//"plusList" is the list for which we will construct expression - it is either extra plus-list or original list.
// In the former case (extra plus-list) we will use it later to construct expression for list
NonTerminal plusList = isPlusList ? list : new NonTerminal(listMember.Name + "+");
plusList.SetFlag(TermFlags.IsList);
plusList.Rule = plusList; // rule => list
if (delimiter != null)
plusList.Rule += delimiter; // rule => list + delim
if (options.IsSet(TermListOptions.AddPreferShiftHint))
plusList.Rule += PreferShiftHere(); // rule => list + delim + PreferShiftHere()
plusList.Rule += listMember; // rule => list + delim + PreferShiftHere() + elem
plusList.Rule |= listMember; // rule => list + delim + PreferShiftHere() + elem | elem
if (isPlusList) {
// if we build plus list - we're almost done; plusList == list
// add trailing delimiter if necessary; for star list we'll add it to final expression
if (allowTrailingDelim)
plusList.Rule |= list + delimiter; // rule => list + delim + PreferShiftHere() + elem | elem | list + delim
} else {
// Setup list.Rule using plus-list we just created
list.Rule = Empty | plusList;
if (allowTrailingDelim)
list.Rule |= plusList + delimiter | delimiter;
plusList.SetFlag(TermFlags.NoAstNode);
list.SetFlag(TermFlags.IsListContainer); //indicates that real list is one level lower
}
return list.Rule;
}
示例4: MakePlusRule
public static BnfExpression MakePlusRule(NonTerminal listNonTerminal, BnfTerm delimiter, BnfTerm listMember, TermListOptions options) {
bool allowTrailingDelimiter = (options & TermListOptions.AllowTrailingDelimiter) != 0;
if (delimiter == null || !allowTrailingDelimiter)
return MakePlusRule(listNonTerminal, delimiter, listMember);
//create plus list
var plusList = new NonTerminal(listMember.Name + "+");
plusList.Rule = MakePlusRule(listNonTerminal, delimiter, listMember);
listNonTerminal.Rule = plusList | plusList + delimiter;
listNonTerminal.SetFlag(TermFlags.IsListContainer);
return listNonTerminal.Rule;
}
示例5: SqlGrammar
//.........这里部分代码省略.........
constraintListOpt.Rule = MakeStarRule(constraintListOpt, constraintDef );
constraintTypeOpt.Rule = PRIMARY + KEY + idlistPar | UNIQUE + idlistPar | NOT + NULL + idlistPar
| "Foreign" + KEY + idlistPar + "References" + Id + idlistPar;
idlistPar.Rule = Empty | "(" + idlist + ")";
idlist.Rule = MakePlusRule(idlist, comma, Id);
//Create Index
createIndexStmt.Rule = CREATE + uniqueOpt + INDEX + Id + ON + Id + orderList + withClauseOpt;
uniqueOpt.Rule = Empty | UNIQUE;
orderList.Rule = MakePlusRule(orderList, comma, orderMember);
orderMember.Rule = Id + orderDirOpt;
orderDirOpt.Rule = Empty | "ASC" | "DESC";
withClauseOpt.Rule = Empty | WITH + PRIMARY | WITH + "Disallow" + NULL | WITH + "Ignore" + NULL;
//Alter
alterStmt.Rule = ALTER + TABLE + Id + alterCmd;
alterCmd.Rule = ADD + COLUMN + fieldDefList + constraintListOpt
| ADD + constraintDef
| DROP + COLUMN + Id
| DROP + CONSTRAINT + Id;
//Drop stmts
dropTableStmt.Rule = DROP + TABLE + Id;
dropIndexStmt.Rule = DROP + INDEX + Id + ON + Id;
//Insert stmt
insertStmt.Rule = INSERT + intoOpt + Id + idlistPar + insertData;
insertData.Rule = selectStmt | VALUES + "(" + exprList + ")";
intoOpt.Rule = Empty | INTO; //Into is optional in MSSQL
//Update stmt
updateStmt.Rule = UPDATE + Id + SET + assignList + whereClauseOpt;
assignList.Rule = MakePlusRule(assignList, comma, assignment);
assignment.Rule = Id + "=" + expression | Id;
//Delete stmt
deleteStmt.Rule = DELETE + FROM + Id + whereClauseOpt;
//Select stmt
selectStmt.Rule = SELECT + selRestrOpt + selList + intoClauseOpt + fromClauseOpt + whereClauseOpt +
groupClauseOpt + havingClauseOpt + orderClauseOpt;
selRestrOpt.Rule = Empty | "ALL" | "DISTINCT";
selList.Rule = columnItemList | "*" | Id | number;
columnItemList.Rule = MakePlusRule(columnItemList, comma, columnItem);
columnItem.Rule = columnSource + aliasOpt;
aliasOpt.Rule = Empty | asOpt + Id;
asOpt.Rule = Empty | AS;
columnSource.Rule = aggregate | Id;
aggregate.Rule = aggregateName + "(" + aggregateArg + ")";
aggregateArg.Rule = expression | "*";
aggregateName.Rule = COUNT | "Avg" | "Min" | "Max" | "StDev" | "StDevP" | "Sum" | "Var" | "VarP";
intoClauseOpt.Rule = Empty | INTO + Id;
fromClauseOpt.Rule = Empty | FROM + idlist + joinChainOpt | FROM + idlist + aliasOpt | FROM + idlist + aliasOpt + joinChainOpt;
joinChainOpt.Rule = Empty | joinSubOpt | joinSubOpt + aliasOpt;
joinSubOpt.Rule = Empty | joinKindOpt + JOIN + idlist | joinKindOpt + JOIN + idlist + joinKindOpt + JOIN + idlist | joinKindOpt + JOIN + idlist + ON + Id + "=" + Id;
joinKindOpt.Rule = Empty | "INNER" | "LEFT" | "RIGHT" | "NATURAL";
whereClauseOpt.Rule = Empty | "WHERE" + expression;
groupClauseOpt.Rule = Empty | "GROUP" + BY + idlist;
havingClauseOpt.Rule = Empty | "HAVING" + expression;
orderClauseOpt.Rule = Empty | "ORDER" + BY + orderList;
//Expression
exprList.Rule = MakePlusRule(exprList, comma, expression);
expression.Rule = term | unExpr | binExpr;// | betweenExpr; //-- BETWEEN doesn't work - yet; brings a few parsing conflicts
term.Rule = subTerm | "'"+subTerm+"'";
subTerm.Rule = Id | string_literal | number | funCall | tuple | parSelectStmt | Id_simple;// | inStmt;
tuple.Rule = "(" + exprList + ")";
parSelectStmt.Rule = "(" + selectStmt + ")";
unExpr.Rule = unOp + term;
unOp.Rule = NOT | "+" | "-" | "~";
binExpr.Rule = expression + binOp + expression;
binOp.Rule = ToTerm("+") | "-" | "*" | "/" | "%" //arithmetic
| "&" | "|" | "^" //bit
| "=" | ">" | "<" | ">=" | "<=" | "<>" | "!=" | "!<" | "!>"
| "AND" | "OR" | "LIKE" | NOT + "LIKE" | "IN" | NOT + "IN" ;
betweenExpr.Rule = expression + notOpt + "BETWEEN" + expression + "AND" + expression;
notOpt.Rule = Empty | NOT;
//funCall covers some psedo-operators and special forms like ANY(...), SOME(...), ALL(...), EXISTS(...), IN(...)
funCall.Rule = Id + "(" + funArgs + ")";
funArgs.Rule = selectStmt | exprList | Empty;
inStmt.Rule = expression + "IN" + "(" + exprList + ")";
//Operators
RegisterOperators(10, "*", "/", "%");
RegisterOperators(9, "+", "-");
RegisterOperators(8, "=", ">", "<", ">=", "<=", "<>", "!=", "!<", "!>", "LIKE", "IN");
RegisterOperators(7, "^", "&", "|");
RegisterOperators(6, NOT);
RegisterOperators(5, "AND");
RegisterOperators(4, "OR");
MarkPunctuation(",", "(", ")");
MarkPunctuation(asOpt, semiOpt);
//Note: we cannot declare binOp as transient because it includes operators "NOT LIKE", "NOT IN" consisting of two tokens.
// Transient non-terminals cannot have more than one non-punctuation child nodes.
// Instead, we set flag InheritPrecedence on binOp , so that it inherits precedence value from it's children, and this precedence is used
// in conflict resolution when binOp node is sitting on the stack
base.MarkTransient(stmt, term, asOpt, aliasOpt, stmtLine, expression, unOp, tuple);
binOp.SetFlag(TermFlags.InheritPrecedence);
}
示例6: SqlExpression
protected NonTerminal SqlExpression()
{
if (sqlExpression != null)
return sqlExpression;
sqlExpression = new NonTerminal("sql_expression");
var sqlUnaryExpression = new NonTerminal("sql_unary_expression", typeof(SqlUnaryExpressionNode));
var sqlBinaryExpression = new NonTerminal("sql_binary_expression", typeof(SqlBinaryExpressionNode));
var sqlBetweenExpression = new NonTerminal("sql_between_expression", typeof(SqlBetweenExpressionNode));
var sqlCaseExpression = new NonTerminal("sql_case_expression", typeof(SqlCaseExpressionNode));
var sqlReferenceExpression = new NonTerminal("sql_reference_expression", typeof(SqlReferenceExpressionNode));
var term = new NonTerminal("term");
var sqlSimpleExpression = new NonTerminal("sql_simple_expression");
var unaryOp = new NonTerminal("unary_op");
var binaryOp = new NonTerminal("binary_op");
var binaryOpSimple = new NonTerminal("binary_op_simple");
var logicalOp = new NonTerminal("logical_op");
var subqueryOp = new NonTerminal("subquery_op");
var caseTestExpressionOpt = new NonTerminal("case_test_expression_opt");
var caseWhenThenList = new NonTerminal("case_when_then_list");
var caseWhenThen = new NonTerminal("case_when_then", typeof(CaseSwitchNode));
var caseElseOpt = new NonTerminal("case_else_opt");
var sqlVarefExpression = new NonTerminal("sql_varef_expression", typeof(SqlVariableRefExpressionNode));
var sqlConstantExpression = new NonTerminal("sql_constant_expression", typeof(SqlConstantExpressionNode));
var functionCallExpression = new NonTerminal("function_call_expression", typeof(SqlFunctionCallExpressionNode));
var functionCallArgsOpt = new NonTerminal("function_call_args_opt");
var functionCallArgsList = new NonTerminal("function_call_args_list");
var notOpt = new NonTerminal("not_opt");
var grouped = new NonTerminal("grouped");
var anyOp = new NonTerminal("any_op");
var allOp = new NonTerminal("all_op");
sqlExpression.Rule = sqlSimpleExpression |
sqlBetweenExpression |
sqlCaseExpression |
SqlQueryExpression();
sqlConstantExpression.Rule = StringLiteral | NumberLiteral | Key("TRUE") | Key("FALSE") | Key("NULL");
sqlSimpleExpression.Rule = term | sqlBinaryExpression | sqlUnaryExpression;
term.Rule = sqlReferenceExpression |
sqlVarefExpression |
sqlConstantExpression |
functionCallExpression |
grouped;
sqlReferenceExpression.Rule = ObjectName();
grouped.Rule = ImplyPrecedenceHere(30) + "(" + sqlExpression + ")";
sqlUnaryExpression.Rule = unaryOp + term;
unaryOp.Rule = Key("NOT") | "+" | "-" | "~";
sqlBinaryExpression.Rule = sqlSimpleExpression + binaryOp + sqlSimpleExpression;
binaryOpSimple.Rule = ToTerm("+") | "-" | "*" | "/" | "%" | ">" | "<" | "=" | "<>";
binaryOp.Rule = binaryOpSimple | allOp | anyOp | logicalOp | subqueryOp;
logicalOp.Rule = Key("AND") | Key("OR") | Key("IS") | Key("IS") + Key("NOT") + "&" | "|";
subqueryOp.Rule = Key("IN") | Key("NOT") + Key("IN");
anyOp.Rule = Key("ANY") + binaryOpSimple;
allOp.Rule = Key("ALL") + binaryOpSimple;
sqlBetweenExpression.Rule = sqlSimpleExpression + notOpt + Key("BETWEEN") + sqlSimpleExpression + Key("AND") +
sqlSimpleExpression;
sqlCaseExpression.Rule = Key("CASE") + caseTestExpressionOpt + caseWhenThenList + caseElseOpt + Key("END");
caseTestExpressionOpt.Rule = Empty | sqlExpression;
caseElseOpt.Rule = Empty | Key("ELSE") + sqlExpression;
caseWhenThenList.Rule = MakePlusRule(caseWhenThenList, caseWhenThen);
caseWhenThen.Rule = Key("WHEN") + sqlExpression + Key("THEN") + sqlExpression;
functionCallExpression.Rule = ObjectName() + functionCallArgsOpt;
functionCallArgsOpt.Rule = Empty | "(" + functionCallArgsList + ")";
functionCallArgsList.Rule = MakeStarRule(functionCallArgsList, Comma, sqlExpression);
sqlVarefExpression.Rule = Colon + Identifier;
notOpt.Rule = Empty | Key("NOT");
MarkTransient(sqlExpression, term, sqlSimpleExpression, grouped, functionCallArgsOpt);
binaryOp.SetFlag(TermFlags.InheritPrecedence);
binaryOpSimple.SetFlag(TermFlags.InheritPrecedence);
logicalOp.SetFlag(TermFlags.InheritPrecedence);
subqueryOp.SetFlag(TermFlags.InheritPrecedence);
unaryOp.SetFlag(TermFlags.InheritPrecedence);
return sqlExpression;
}
示例7: STLGrammar
public STLGrammar()
: base(false)
{
#region Letters and Digits
/*
letter A | B | .. | Z | a | b | .. |z
digit 0 | 1 | .. | 9
bit digit 0|1| .. |7
binary digit 0 | 1 | _
hex digit digit | A | .. | F | a | .. | f | _
character letter | ASCII 128 | .. | ASCII 255 | _
first identifier character character | space
other identifier character character | digit | space
Local Variables (Local ToTerms) [ # ] identifier
*/
var semi = ToTerm(";", "semi");
var semi_opt = new NonTerminal("semi?", Empty | semi);
var dot = ToTerm(".", "dot");
var comma = ToTerm(",", "comma");
var comma_opt = new NonTerminal("comma_opt", Empty | comma);
var equal = ToTerm("=", "equal");
var digits = new NumberLiteral("decimal", NumberOptions.IntOnly | NumberOptions.DisableQuickParse);
var binarys = new NumberLiteral("binary", NumberOptions.IntOnly | NumberOptions.DisableQuickParse | NumberOptions.Binary | NumberOptions.AllowUnderscore);
var octals = new NumberLiteral("octal", NumberOptions.IntOnly | NumberOptions.DisableQuickParse | NumberOptions.Octal);
var hexs = new NumberLiteral("hex", NumberOptions.IntOnly | NumberOptions.DisableQuickParse | NumberOptions.Hex | NumberOptions.AllowUnderscore);
var identifier = TerminalFactory.CreateCSharpIdentifier("identifier");
var global_variable = new NonTerminal("global_variable", typeof(Variable));
var local_variable = new NonTerminal("local_variable", typeof(Variable));
KeyTerm colon = ToTerm(":", "colon");
binarys.AddPrefix("2#", NumberOptions.Binary);
hexs.AddPrefix("16#", NumberOptions.Hex);
global_variable.SetFlag(TermFlags.IsConstant);
local_variable.SetFlag(TermFlags.IsConstant);
global_variable.Rule = identifier;
local_variable.Rule = "#" + identifier;
#endregion
#region Program Comments
/*
comment characters printable character CR
comment // { comment characters } CR
*/
var comment = new CommentTerminal("comment", "//", "\r", "\n");
var comment_opt = new NonTerminal("comment?");
var line_comment = new NonTerminal("line_comment");
var multi_line_comment = new NonTerminal("multi_line_comment");
var empty_line = new NonTerminal("empty_line");
var empty_lines = new NonTerminal("empty_line+");
var empty_lines_opt = new NonTerminal("empty_line*");
multi_line_comment.SetFlag(TermFlags.IsList);
empty_lines.SetFlag(TermFlags.IsList);
empty_lines_opt.SetFlag(TermFlags.IsListContainer);
comment_opt.Rule = comment | Empty;
line_comment.Rule = comment + NewLine;
multi_line_comment.Rule = multi_line_comment + line_comment | line_comment;
empty_line.Rule = NewLine | line_comment;
empty_lines.Rule = empty_lines + empty_line | empty_line;
empty_lines_opt.Rule = empty_lines | Empty;
#endregion
#region Instruction mnemonic terminal
// Bit logic
var bit_logic_mnemonic = new NonTerminal("bit_logic_mnemonic");
var clock_mnemonic = new NonTerminal("clock_mnemonic");
var communication_mnemonic = new NonTerminal("communication_mnemonic");
var compare_mnemonic = new NonTerminal("compare_mnemonic");
var convert_mnemonic = new NonTerminal("convert_mnemonic");
var counter_mnemonic = new NonTerminal("counter_mnemonic");
var floatpoint_math_mnemonic = new NonTerminal("floatpoint_math_mnemonic");
var integer_math_mnemonic = new NonTerminal("integer_math_mnemonic");
var logical_mnemonic = new NonTerminal("logical_mnemonic");
var move_mnemonic = new NonTerminal("move_mnemonic");
var program_control_mnemonic = new NonTerminal("program_control_mnemonic");
var shift_rotate_mnemonic = new NonTerminal("shift_rotate_mnemonic");
var string_mnemonic = new NonTerminal("string_mnemonic");
var table_mnemonic = new NonTerminal("table_mnemonic");
var timer_mnemonic = new NonTerminal("timer_mnemonic");
var subroutine_mnemonic = new NonTerminal("subroutine_mnemonic");
bit_logic_mnemonic.Rule =
ToTerm("LD") | "A" | "O" |
"LDN" | "AN" | "ON" |
"LDI" | "AI" | "OI" |
"LDNI" | "ANI" | "ONI" |
"NOT" | "EU" | "ED" | "ALD" | "OLD" |
"LPS" | "LDS" | "LRD" | "LPP" | "=" | "=I" |
"S" | "SI" | "R" | "RI" | "AENO" | "NOP";
clock_mnemonic.Rule =
ToTerm("TODR") | "TODW" | "TODRX" | "TODWX";
communication_mnemonic.Rule =
ToTerm("XMT") | "RCV" | "NETR" | "NETW" | "GPA" | "SPA";
compare_mnemonic.Rule =
ToTerm("LDB=") | "AB=" | "OB=" |
"LDB<>" | "AB<>" | "OB<>" |
"LDB>=" | "AB>=" | "OB>=" |
//.........这里部分代码省略.........
示例8: MakeListRule
protected BnfExpression MakeListRule(NonTerminal list, BnfTerm delimiter, BnfTerm listMember, TermListOptions options = TermListOptions.PlusList)
{
//If it is a star-list (allows empty), then we first build plus-list
var isStarList = options.IsSet(TermListOptions.AllowEmpty);
NonTerminal plusList = isStarList ? new NonTerminal(listMember.Name + "+") : list;
//"list" is the real list for which we will construct expression - it is either extra plus-list or original listNonTerminal.
// In the latter case we will use it later to construct expression for listNonTerminal
plusList.Rule = plusList; // rule => list
if (delimiter != null)
plusList.Rule += delimiter; // rule => list + delim
if (options.IsSet(TermListOptions.AddPreferShiftHint))
plusList.Rule += PreferShiftHere(); // rule => list + delim + PreferShiftHere()
plusList.Rule += listMember; // rule => list + delim + PreferShiftHere() + elem
plusList.Rule |= listMember; // rule => list + delim + PreferShiftHere() + elem | elem
//trailing delimiter
if (options.IsSet(TermListOptions.AllowTrailingDelimiter) & delimiter != null)
plusList.Rule |= list + delimiter; // => list + delim + PreferShiftHere() + elem | elem | list + delim
// set Rule value
plusList.SetFlag(TermFlags.IsList);
//If we do not use exra list - we're done, return list.Rule
if (plusList == list)
return list.Rule;
// Let's setup listNonTerminal.Rule using plus-list we just created
//If we are here, TermListOptions.AllowEmpty is set, so we have star-list
list.Rule = Empty | plusList;
plusList.SetFlag(TermFlags.NoAstNode);
list.SetFlag(TermFlags.IsListContainer); //indicates that real list is one level lower
return list.Rule;
}
示例9: SpecParser
//.........这里部分代码省略.........
Terminal explicitUnsignedIntegerConstant = new NumberLiteral("explicitUnsignedIntegerConstant", NumberOptions.IntOnly | NumberOptions.NoDotAfterInt);//new NonTerminal("explicitUnsignedIntegerConstant");
//3. BNF Rules
//Programs and declarations
program.Rule = MakeStarRule(program, declarationOrStatementsInMainProgram);
declarationOrStatementsInMainProgram.Rule = declaration | statement | subprogramDeclaration;
declaration.Rule = constantDeclaration | variableDeclaration | typeDeclaration;
constantDeclaration.Rule = ToTerm("const") + id + ":=" + expn
| "const" + id + ":" + typeSpec + ":=" + initializingValue;
initializingValue.Rule = expn | "init" + "(" + MakePlusRule(initializingValue, ToTerm(","), initializingValue) + ")";
variableDeclaration.Rule = ToTerm("var") + MakePlusRule(variableDeclaration, ToTerm(","), id) + ":=" + expn
| "var" + MakePlusRule(variableDeclaration, ToTerm(","), id) + ":" + typeSpec + (":=" + initializingValue | Empty);
//Types
typeDeclaration.Rule = ToTerm("type") + id + ":" + typeSpec;
typeSpec.Rule = standardType | subrangeType | arrayType | recordType | namedType;
standardType.Rule = ToTerm("int") | "real" | "boolean" | "string" + ("(" + compileTimeExpn + ")" | Empty);
subrangeType.Rule = compileTimeExpn + ".." + expn;
arrayType.Rule = ToTerm("array") + MakePlusRule(arrayType, ToTerm(","), indexType) + "of" + typeSpec;
indexType.Rule = subrangeType | namedType;
recordType.Rule = ToTerm("record") + MakePlusRule(recordType, MakePlusRule(recordType, ToTerm(","), id) + ":" + typeSpec) + "end" + "record";
namedType.Rule = id;
//Subprograms
subprogramDeclaration.Rule = subprogramHeader + subprogramBody;
subprogramHeader.Rule = ToTerm("procedure") + id + ("(" + MakePlusRule(subprogramHeader, ToTerm(","), parameterDeclaration) + ")" | Empty)
| "function" + id + ("(" + MakePlusRule(subprogramHeader, ToTerm(","), parameterDeclaration) + ")" | Empty) + ":" + typeSpec;
parameterDeclaration.Rule = (ToTerm("var") | Empty) + MakePlusRule(parameterDeclaration, ToTerm(","), id) + ":" + parameterType;
parameterType.Rule = typeSpec | "string" + "(" + "*" + ")"
| "array" + MakePlusRule(parameterType, ToTerm(","), compileTimeExpn + ".." + "*") + "of" + typeSpec
| "array" + MakePlusRule(parameterType, ToTerm(","), compileTimeExpn + ".." + "*") + "of" + "string" + "(" + "*" + ")";
subprogramBody.Rule = declarationsAndStatements + "end" + id;
//Statements and Input/Output
declarationsAndStatements.Rule = MakePlusRule(declarationsAndStatements, declarationOrStatement);
declarationOrStatement.Rule = declaration | statement;
statement.Rule = variableReference + ":=" + expn
| procedureCall
| "assert" + booleanExpr
| "result" + expn
| ifStatement
| loopStatement
| "exit" + ("when" + booleanExpr | Empty)
| caseStatement
| forStatement
| putStatement
| getStatement
| openStatement
| closeStatement;
procedureCall.Rule = reference;
ifStatement.Rule = ToTerm("if") + booleanExpr + "then" + declarationsAndStatements + MakeStarRule(ifStatement, ToTerm("elsif") + booleanExpr + "then" + declarationsAndStatements) + ("else" + declarationsAndStatements | Empty) + "end" + "if";
loopStatement.Rule = ToTerm("loop") + declarationsAndStatements + "end" + "loop";
caseStatement.Rule = ToTerm("case") + expn + "of" + MakePlusRule(caseStatement, ToTerm("label") + MakePlusRule(caseStatement, ToTerm(","), compileTimeExpn) + ":" + declarationsAndStatements) + ("label" + ":" + declarationsAndStatements | Empty) + "end" + "case";
forStatement.Rule = ToTerm("for") + (id | Empty) + ":" + expn + ".." + expn + ("by" + expn | Empty) + declarationsAndStatements + "end" + "for"
| "for" + "decreasing" + (id | Empty) + ":" + expn + ".." + expn + ("by" + expn | Empty) + declarationsAndStatements + "end" + "for";
putStatement.Rule = ToTerm("put") + (":" + streamNumber + "," | Empty) + MakePlusRule(putStatement, ToTerm(","), putItem) + (".." | Empty);
putItem.Rule = expn + (":" + widthExpn + (":" + fractionWidth + (":" + exponentWidth | Empty) | Empty) | Empty) | "skip";
getStatement.Rule = ToTerm("get") + (":" + streamNumber + "," | Empty) + MakePlusRule(getStatement, ToTerm(","), getItem);
getItem.Rule = variableReference | "skip" | variableReference + ":" + "*" | variableReference + ":" + widthExpn;
openStatement.Rule = ToTerm("open") + ":" + fileNumberVariable + "," + fileName + "," + MakePlusRule(openStatement, ToTerm(","), capability);
capability.Rule = ToTerm("put") | "get";
closeStatement.Rule = ToTerm("close") + ":" + fileNumber;
streamNumber.Rule = widthExpn.Rule = fractionWidth.Rule = exponentWidth.Rule = fileNumber.Rule = expn;
//Following are guesses as to the rules, since the spec does not state the rules for them
fileNumberVariable.Rule = variableReference;
fileName.Rule = explicitStringConstant | variableReference;
//References and Expressions
variableReference.Rule = reference;
reference.Rule = id | reference + componentSelector;
componentSelector.Rule = "(" + MakePlusRule(componentSelector, ToTerm(","), expn) + ")" | "." + id;
booleanExpr.Rule = compileTimeExpn.Rule = expn;
expn.Rule = reference | explicitConstant | substring | expn + infixOperator + expn | prefixOperator + expn | "(" + expn + ")";
expn.SetFlag(TermFlags.InheritPrecedence);
explicitConstant.Rule = explicitUnsignedIntegerConstant | explicitUnsignedRealConstant | explicitStringConstant | "true" | "false";
infixOperator.Rule = ToTerm("+") | "-" | "*" | "/" | "div" | "mod" | "**" | "<" | ">" | "=" | "<=" | ">=" | "not=" | "and" | "or";
//prefixOperator.Precedence = 70;
prefixOperator.Rule = ToTerm("+") | "-" | "not";
substring.Rule = reference + "(" + substringPosition + (".." + substringPosition | Empty) + ")";
substringPosition.Rule = expn | "*" + ("-" + expn | Empty);
this.Root = program;
//4. Set operator precendence and associativity
RegisterOperators(80, Associativity.Left, "**");//this is VERY odd, but Turing simplifies associativity by saying it's all left associative
RegisterOperators(60, "*","/","div","mod");
RegisterOperators(50, "+", "-");
RegisterOperators(40, "<", ">", "=", "<=", ">=", "not=");
RegisterOperators(30, "not");
RegisterOperators(20, "and");
RegisterOperators(10, "or");
//5. Register Parenthesis as punctuation symbols so they will not appear in the syntax tree
MarkPunctuation("(", ")", ",");
RegisterBracePair("(", ")");
//MarkTransient(Expr, BinOp, ParExpr);
this.LanguageFlags = LanguageFlags.NewLineBeforeEOF;
}
示例10: ID3SQLGrammar
private ID3SQLGrammar() : base(false)
{
Terminal numberTerm = new NumberLiteral(NumberTermName);
Terminal stringLiteralTerm = new StringLiteral(StringLiteralTermName, "'", StringOptions.AllowsDoubledQuote);
Terminal idTerm = TerminalFactory.CreateSqlExtIdentifier(this, IdTermName);
NonTerminal statementNonTerm = new NonTerminal(StatementNonTermName);
NonTerminal selectStatementNonTerm = new NonTerminal(SelectStatementNonTermName);
NonTerminal updateStatementNonTerm = new NonTerminal(UpdateStatementNonTermName);
NonTerminal deleteStatementNonTerm = new NonTerminal(DeleteStatementNonTermName);
NonTerminal assignListNonTerm = new NonTerminal(AssignListNonTermName);
NonTerminal whereClauseNonTerm = new NonTerminal(WhereClauseNonTermName);
NonTerminal assignmentNonTerm = new NonTerminal(AssignmentNonTermName);
NonTerminal expressionNonTerm = new NonTerminal(ExpressionNonTermName);
NonTerminal expressionListNonTerm = new NonTerminal(ExpressionListNonTermName);
NonTerminal selectListNonTerm = new NonTerminal(SelectListNonTermName);
NonTerminal columnItemListNonTerm = new NonTerminal(ColumnItemListNonTermName);
NonTerminal termNonTerm = new NonTerminal(TermNonTermName);
NonTerminal unaryExpressionNonTerm = new NonTerminal(UnaryExpressionNonTermName);
NonTerminal unaryOperatorNonTerm = new NonTerminal(UnaryOperatorNonTermName);
NonTerminal binaryExpressionNonTerm = new NonTerminal(BinaryExpressionNonTermName);
NonTerminal binaryOperatorNonTerm = new NonTerminal(BinaryOperatorNonTermName);
NonTerminal notOperatorNonTerm = new NonTerminal(NotOperatorNonTermName);
NonTerminal inStatementNonTerm = new NonTerminal(InStatementNonTermName);
this.Root = statementNonTerm;
statementNonTerm.Rule = selectStatementNonTerm | updateStatementNonTerm | deleteStatementNonTerm;
//Select statement
selectStatementNonTerm.Rule = ToTerm("SELECT") + selectListNonTerm + whereClauseNonTerm;
selectListNonTerm.Rule = columnItemListNonTerm | "*";
columnItemListNonTerm.Rule = MakePlusRule(columnItemListNonTerm, ToTerm(","), idTerm);
whereClauseNonTerm.Rule = Empty | "WHERE" + expressionNonTerm;
//Update statement
updateStatementNonTerm.Rule = ToTerm("UPDATE") + "SET" + assignListNonTerm + whereClauseNonTerm;
assignListNonTerm.Rule = MakePlusRule(assignListNonTerm, ToTerm(","), assignmentNonTerm);
assignmentNonTerm.Rule = idTerm + "=" + expressionNonTerm;
//Delete statement
deleteStatementNonTerm.Rule = ToTerm("DELETE") + whereClauseNonTerm;
//Expression
expressionListNonTerm.Rule = MakePlusRule(expressionListNonTerm, ToTerm(","), expressionNonTerm);
expressionNonTerm.Rule = termNonTerm | unaryExpressionNonTerm | binaryExpressionNonTerm;
termNonTerm.Rule = idTerm | stringLiteralTerm | numberTerm;
unaryExpressionNonTerm.Rule = unaryOperatorNonTerm + termNonTerm;
unaryOperatorNonTerm.Rule = ToTerm("NOT") | "-";
binaryExpressionNonTerm.Rule = expressionNonTerm + binaryOperatorNonTerm + expressionNonTerm;
binaryOperatorNonTerm.Rule = ToTerm("=") | ">" | "<" | ">=" | "<=" | "!="
| "AND" | "OR" | "LIKE" | "NOT" + "LIKE" | "IN" | "NOT" + "IN";
notOperatorNonTerm.Rule = Empty | "NOT";
inStatementNonTerm.Rule = expressionNonTerm + "IN" + "(" + expressionListNonTerm + ")";
//Operators
RegisterOperators(10, "*", "/", "%");
RegisterOperators(9, "+", "-");
RegisterOperators(8, "=", ">", "<", ">=", "<=", "<>", "!=", "!<", "!>", "LIKE", "IN");
RegisterOperators(7, "^", "&", "|");
RegisterOperators(6, "NOT");
RegisterOperators(5, "AND");
RegisterOperators(4, "OR");
MarkPunctuation(",", "(", ")");
//Note: we cannot declare binOp as transient because it includes operators "NOT LIKE", "NOT IN" consisting of two tokens.
// Transient non-terminals cannot have more than one non-punctuation child nodes.
// Instead, we set flag InheritPrecedence on binOp , so that it inherits precedence value from it's children, and this precedence is used
// in conflict resolution when binOp node is sitting on the stack
base.MarkTransient(statementNonTerm, termNonTerm, expressionNonTerm, unaryOperatorNonTerm);
binaryOperatorNonTerm.SetFlag(TermFlags.InheritPrecedence);
}
示例11: ExpressionGrammar
public ExpressionGrammar()
: base(false)
{
//SQL is case insensitive
//Terminals
var comment = new CommentTerminal("comment", "/*", "*/");
var lineComment = new CommentTerminal("line_comment", "--", "\n", "\r\n");
NonGrammarTerminals.Add(comment);
NonGrammarTerminals.Add(lineComment);
var numberLiteral = new NumberLiteral("number", NumberOptions.AllowSign | NumberOptions.AllowStartEndDot) {DefaultIntTypes = new [] {TypeCode.Int32, TypeCode.Int64 }};
var stringLiteral = new StringLiteral("string", "'", StringOptions.AllowsDoubledQuote | StringOptions.AllowsLineBreak | StringOptions.AllowsAllEscapes);
var idSimple = CreateSqlExtIdentifier("id_simple"); //covers normal identifiers (abc) and quoted id's ([abc d], "abc d")
var commaTerm = ToTerm(",");
var dotTerm = ToTerm(".");
var notTerm = ToTerm("NOT");
//Non-terminals
var id = new NonTerminal("Id");
var idlist = new NonTerminal("idlist");
var idlistPar = new NonTerminal("idlistPar");
var tuple = new NonTerminal("tuple");
var expression = new NonTerminal("expression");
var exprList = new NonTerminal("exprList");
var caseStmt = new NonTerminal("case");
var caseVariable = new NonTerminal("caseVariable");
var caseWhenList = new NonTerminal("caseWhenList");
var caseWhenThen = new NonTerminal("caseWhenThen");
var caseDefaultOpt = new NonTerminal("caseDefault");
var term = new NonTerminal("term");
var unExpr = new NonTerminal("unExpr");
var unOp = new NonTerminal("unOp");
var binExpr = new NonTerminal("binExpr");
var binOp = new NonTerminal("binOp");
var betweenExpr = new NonTerminal("betweenExpr");
//var inExpr = new NonTerminal("inExpr");
var notOpt = new NonTerminal("notOpt");
var funCall = new NonTerminal("funCall");
var funArgs = new NonTerminal("funArgs");
//var inStmt = new NonTerminal("inStmt");
//BNF Rules
Root = expression;
//ID
id.Rule = MakePlusRule(id, dotTerm, idSimple);
idlistPar.Rule = "(" + idlist + ")";
idlist.Rule = MakePlusRule(idlist, commaTerm, id);
caseWhenThen.Rule = "WHEN" + exprList + "THEN" + expression;
caseWhenList.Rule = MakePlusRule(caseWhenList, caseWhenThen);
caseDefaultOpt.Rule = Empty | "ELSE" + expression;
caseVariable.Rule = Empty | expression;
caseStmt.Rule = "CASE" + caseVariable + caseWhenList + caseDefaultOpt + "END";
//Expression
exprList.Rule = MakePlusRule(exprList, commaTerm, expression);
expression.Rule = term | unExpr | binExpr | caseStmt | betweenExpr; //-- BETWEEN brings a few parsing conflicts, use parentheses
tuple.Rule = "(" + exprList + ")";
term.Rule = id | stringLiteral | numberLiteral | funCall | tuple;// | inStmt;
unExpr.Rule = unOp + term | term + "IS" + "NULL" | term + "IS" + "NOT" + "NULL";
unOp.Rule = notTerm | "+" | "-" | "~";
binExpr.Rule = expression + binOp + expression;
binOp.Rule = ToTerm("+") | "-" | "*" | "/" | "%" //arithmetic
| "&" | "|" | "^" //bit
| "=" | ">" | "<" | ">=" | "<=" | "<>" | "!=" | "!<" | "!>"
| "AND" | "OR" | "XOR" | "LIKE" | notTerm + "LIKE" | "IN" | notTerm + "IN";
betweenExpr.Rule = expression + notOpt + "BETWEEN" + expression + "AND" + expression;
notOpt.Rule = Empty | notTerm;
//funCall covers some psedo-operators and special forms like ANY(...), SOME(...), ALL(...), EXISTS(...), IN(...)
funCall.Rule = id + "(" + funArgs + ")";
funArgs.Rule = Empty | exprList;
//inStmt.Rule = expression + "IN" + "(" + exprList + ")";
//Operators
RegisterOperators(10, "*", "/", "%");
RegisterOperators(9, "+", "-");
RegisterOperators(8, "=", ">", "<", ">=", "<=", "<>", "!=", "!<", "!>", "LIKE", "IN", "BETWEEN");
RegisterOperators(7, "^", "&", "|");
RegisterOperators(6, notTerm);
RegisterOperators(5, "AND");
RegisterOperators(4, "OR", "XOR");
MarkPunctuation(",", "(", ")");
//Note: we cannot declare binOp as transient because it includes operators "NOT LIKE", "NOT IN" consisting of two tokens.
// Transient non-terminals cannot have more than one non-punctuation child nodes.
// Instead, we set flag InheritPrecedence on binOp , so that it inherits precedence value from it's children, and this precedence is used
// in conflict resolution when binOp node is sitting on the stack
MarkTransient(term, expression, unOp);
binOp.SetFlag(TermFlags.InheritPrecedence);
SnippetRoots.Add(expression);
}