public override bool Match(ConflictResolutionArgs args) { string identifier = args.Context.PreviousToken.Text; //var type = DeclarationManager.Instance.Find(args.Context, identifier); bool result; //if (type == DeclarationType.NotFound) //{ result = false; if (isExpectingType) { // We are probably in a type cast if (args.Context.CurrentToken.Text == ")") { // Verify that next token is an identifier, a number or left parenthesis => Then the current expression is certainly a cast args.Scanner.BeginPreview(); Token nextToken; do { nextToken = args.Scanner.GetToken(); } while (nextToken.Terminal.FlagIsSet(TermFlags.IsNonGrammar) && nextToken.Terminal != Grammar.Eof); var nextTokenName = nextToken.Terminal.Name; if (nextTokenName == "identifier" || nextTokenName == "integer_literal" || nextTokenName == "float_literal" || nextTokenName == "(") result = true; args.Scanner.EndPreview(true); } } if (!result && nextGrammarHint != null) { result = nextGrammarHint.Match(args); } // In case that we found something, use the reduce production where this hint is used if (result) { args.Result = ParserActionType.Reduce; args.ReduceProduction = null; } //} else if (isExpectingType) // result = type == DeclarationType.TypeName; //else // result = type == DeclarationType.Variable; return result; }
public override bool Match(ConflictResolutionArgs args) { string identifier = args.Context.PreviousToken.Text; //var type = DeclarationManager.Instance.Find(args.Context, identifier); bool result; //if (type == DeclarationType.NotFound) //{ result = false; if (isExpectingType) { // We are probably in a type cast if (args.Context.CurrentToken.Text == ")") { // Verify that next token is an identifier, a number or left parenthesis => Then the current expression is certainly a cast args.Scanner.BeginPreview(); Token nextToken; do { nextToken = args.Scanner.GetToken(); }while (nextToken.Terminal.FlagIsSet(TermFlags.IsNonGrammar) && nextToken.Terminal != Grammar.Eof); var nextTokenName = nextToken.Terminal.Name; if (nextTokenName == "identifier" || nextTokenName == "integer_literal" || nextTokenName == "float_literal" || nextTokenName == "(") { result = true; } args.Scanner.EndPreview(true); } } if (!result && nextGrammarHint != null) { result = nextGrammarHint.Match(args); } // In case that we found something, use the reduce production where this hint is used if (result) { args.Result = ParserActionType.Reduce; args.ReduceProduction = null; } //} else if (isExpectingType) // result = type == DeclarationType.TypeName; //else // result = type == DeclarationType.Variable; return(result); }
public override bool Match(ConflictResolutionArgs args) { if (args.Context.CurrentParserInput.Term.Name == "<") { args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; bool isKeyword = false; while (true) { // Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); }while ((preview.Terminal.FlagIsSet(TermFlags.IsNonGrammar) || skipTokens.Contains(preview.Terminal)) && preview.Terminal != Grammar.Eof); isKeyword = preview.EditorInfo.Color == TokenColor.Keyword; // See what did we find previewSym = preview.Terminal.Name; if (previewSym == "<") { ltCount++; } else if (previewSym == ">" && ltCount > 0) { ltCount--; } else { break; } } args.Scanner.EndPreview(true); // if we see ">", then it is type argument, not operator // if previewSym == ">" then shift else reduce if (previewSym == ">" || isKeyword) { args.Result = ParserActionType.Shift; return(true); } else { args.Result = ParserActionType.Reduce; return(true); } } return(false); }
/// <summary> /// Override this method to provide custom conflict resolution; for example, custom code may decide proper shift or reduce /// action based on preview of tokens ahead. /// </summary> public virtual void OnResolvingConflict(ConflictResolutionArgs args) { //args.Result is Shift by default }
public override bool Match(ConflictResolutionArgs args) { return(resolver(args)); }
public override void OnResolvingConflict(ConflictResolutionArgs args) { switch (args.Context.CurrentParserInput.Term.Name) { case "[": { args.Scanner.BeginPreview(); var preview = args.Scanner.GetToken(); string previewSym = preview.Terminal.Name; args.Result = previewSym == "]" ? ParserActionType.Reduce : ParserActionType.Shift; args.Scanner.EndPreview(true); return; } case "dot": { args.Scanner.BeginPreview(); var preview = args.Scanner.GetToken(); string previewSym = preview.Text; if (previewSym == "<") { // skip over any type arguments int depth = 0; do { if (previewSym == "<") { ++depth; } else if (previewSym == ">") { --depth; } preview = args.Scanner.GetToken(); previewSym = preview.Text; } while (depth > 0 && preview.Terminal != Eof); } switch (previewSym) { case "new": case "super": case "this": args.Result = ParserActionType.Reduce; break; default: args.Result = ParserActionType.Shift; break; } args.Scanner.EndPreview(true); return; } case "lt": { args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; while (true) { //Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); } while (mSkipTokensInPreview.Contains(preview.Terminal) && preview.Terminal != Eof); //See what did we find previewSym = preview.Terminal.Name; if ((previewSym == "<") || (previewSym == "lt")) { ltCount++; } else if (((previewSym == ">") || (previewSym == "gt")) && ltCount > 0) { ltCount--; continue; } else break; } //if we see ">", then it is type argument, not operator if ((previewSym == ">") || (previewSym == "gt")) { args.Result = ParserActionType.Shift; } else { args.Result = ParserActionType.Reduce; } args.Scanner.EndPreview(true); //keep previewed tokens; important to keep ">>" matched to two ">" symbols, not one combined symbol (see method below) return; } } }
private void ExecuteConflictAction(ParserAction action) { var args = new ConflictResolutionArgs(Context, action); _grammar.OnResolvingConflict(args); switch(args.Result) { case ParserActionType.Reduce: ExecuteReduce(new ParserAction(ParserActionType.Reduce, null, args.ReduceProduction)); break; case ParserActionType.Operator: ExecuteOperatorAction(new ParserAction(ParserActionType.Operator, action.NewState, args.ReduceProduction)); break; case ParserActionType.Shift: default: ExecuteShift(action); break; } Context.AddTrace(O2_Misc_Microsoft_MPL_Libs.Irony_Parser.Resources.MsgTraceConflictResolved); }
private void ExecuteConflictAction(ParserAction action) { var args = new ConflictResolutionArgs(_context, action); _grammar.OnResolvingConflict(args); switch(args.Result) { case ParserActionType.Reduce: ExecuteReduce(args.ReduceProduction); break; case ParserActionType.Operator: ExecuteOperatorAction(action.NewState, args.ReduceProduction); break; case ParserActionType.Shift: default: ExecuteShift(action.NewState); break; } if (_currentTraceEntry != null) { _currentTraceEntry.Message = "(Conflict resolved in code) " + _currentTraceEntry.Message; } }
private void ExecuteConflictAction(ParserAction action) { var args = new ConflictResolutionArgs(_context, action); _grammar.OnResolvingConflict(args); switch(args.Result) { case ParserActionType.Reduce: ExecuteReduce(args.ReduceProduction); break; case ParserActionType.Operator: ExecuteOperatorAction(action.NewState, args.ReduceProduction); break; case ParserActionType.Shift: default: ExecuteShift(action.NewState); break; } }
/* The shift-reduce conflict for "<" symbol is the problem of deciding what is "<" symbol in the input - is it * opening brace for generic reference, or logical operator. The following is a printout of parser state that has a conflict * The handling code need to run ahead and decide a proper action: if we see ">", then it is a generic bracket and we do shift; * otherwise, it is an operator and we make Reduce * State S188 (Inadequate) Shift items: member_access_segments_opt -> member_access_segments_opt ·member_access_segment member_access_segment -> ·. Identifier member_access_segment -> ·array_indexer array_indexer -> ·[ expression_list ] member_access_segment -> ·argument_list_par argument_list_par -> ·( argument_list_opt ) member_access_segment -> ·type_argument_list type_argument_list -> ·_<_ type_ref_list > _<_ -> ·< Reduce items: member_access -> identifier_ext member_access_segments_opt · [? , ) : ; } ] Identifier ++ -- || && | ^ & == != > <= >= << >> + - * / % = += -= *= /= %= &= |= ^= <<= >>= is as ?? <] Shifts: member_access_segment->S220, .->S221, array_indexer->S222, [->S223, argument_list_par->S224, (->S225, type_argument_list->S226, _<_->S59, */ //Here is an elaborate generic declaration which can be used as a good test. Perfectly legal, uncomment it to check that c# // accepts it: // List<Dictionary<string, object[,]>> genericVar; public override void OnResolvingConflict(ConflictResolutionArgs args) { switch(args.Context.CurrentParserInput.Term.Name) { case "<": args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; while(true) { //Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); } while (_skipTokensInPreview.Contains(preview.Terminal) && preview.Terminal != base.Eof); //See what did we find previewSym = preview.Terminal.Name; if (previewSym == "<") ltCount++; else if (previewSym == ">" && ltCount > 0) { ltCount--; continue; } else break; } //if we see ">", then it is type argument, not operator if (previewSym == ">") args.Result = ParserActionType.Shift; else args.Result = ParserActionType.Reduce; args.Scanner.EndPreview(true); //keep previewed tokens; important to keep ">>" matched to two ">" symbols, not one combined symbol (see method below) return; } }
public override void OnResolvingConflict(ConflictResolutionArgs args) { switch (args.Context.CurrentParserInput.Term.Name) { case "[": { args.Scanner.BeginPreview(); var preview = args.Scanner.GetToken(); string previewSym = preview.Terminal.Name; args.Result = previewSym == "]" ? ParserActionType.Reduce : ParserActionType.Shift; args.Scanner.EndPreview(true); return; } case "dot": { args.Scanner.BeginPreview(); var preview = args.Scanner.GetToken(); string previewSym = preview.Text; if (previewSym == "<") { // skip over any type arguments int depth = 0; do { if (previewSym == "<") { ++depth; } else if (previewSym == ">") { --depth; } preview = args.Scanner.GetToken(); previewSym = preview.Text; } while (depth > 0 && preview.Terminal != Eof); } switch (previewSym) { case "new": case "super": case "this": args.Result = ParserActionType.Reduce; break; default: args.Result = ParserActionType.Shift; break; } args.Scanner.EndPreview(true); return; } case "lt": { args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; while (true) { //Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); } while (mSkipTokensInPreview.Contains(preview.Terminal) && preview.Terminal != Eof); //See what did we find previewSym = preview.Terminal.Name; if ((previewSym == "<") || (previewSym == "lt")) { ltCount++; } else if (((previewSym == ">") || (previewSym == "gt")) && ltCount > 0) { ltCount--; continue; } else { break; } } //if we see ">", then it is type argument, not operator if ((previewSym == ">") || (previewSym == "gt")) { args.Result = ParserActionType.Shift; } else { args.Result = ParserActionType.Reduce; } args.Scanner.EndPreview(true); //keep previewed tokens; important to keep ">>" matched to two ">" symbols, not one combined symbol (see method below) return; } } }
public abstract bool Match(ConflictResolutionArgs args);
public override bool Match(ConflictResolutionArgs args) { return resolver(args); }
public override bool Match(ConflictResolutionArgs args) { if (args.Context.CurrentParserInput.Term.Name == "<") { args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; bool isKeyword = false; while (true) { // Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); } while ((preview.Terminal.FlagIsSet(TermFlags.IsNonGrammar) || skipTokens.Contains(preview.Terminal)) && preview.Terminal != Grammar.Eof); isKeyword = preview.EditorInfo.Color == TokenColor.Keyword; // See what did we find previewSym = preview.Terminal.Name; if (previewSym == "<") { ltCount++; } else if (previewSym == ">" && ltCount > 0) { ltCount--; } else break; } args.Scanner.EndPreview(true); // if we see ">", then it is type argument, not operator // if previewSym == ">" then shift else reduce if (previewSym == ">" || isKeyword) { args.Result = ParserActionType.Shift; return true; } else { args.Result = ParserActionType.Reduce; return true; } } return false; }