Example #1
0
 public AcInformation(List<String> AcLines, ParserReturn AcMetadata, String ToBeAced, int IndexOfLastDelimiter)
 {
     _AcLines                = AcLines;
     _AcMetadata             = AcMetadata;
     _ToBeAced               = ToBeAced;
     _IndexOfLastDelimiter   = IndexOfLastDelimiter;
 }
Example #2
0
        public bool Equals(ParserReturn p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }

            return (this.description == p.description) && (this.isComplete == p.isComplete) && (this.isUsedForAutoCompletion == p.isUsedForAutoCompletion)
                && (this.name == p.name) && (this.typeOfLiteral == p.typeOfLiteral);
        }
Example #3
0
        private List<ParserReturn> GetPossibleTerminals(CompilerContext MyCompilerContext, List<Token> FilteredTokenStream, Boolean _GetAllTerminals)
        {
            #region data
                                _context            = MyCompilerContext;
                                _caseSensitive      = _context.Compiler.Grammar.CaseSensitive;
                                _input              = FilteredTokenStream.GetEnumerator();
            ActionRecord            action;
            List<String>            PossibleTerminals   = new List<string>();
            List<String>            RedundantTerminals  = new List<string>();
            String                  aActionKey          = "";
            ActionRecord            aActionValue;
            String                  aActionType         = "";
            List<ParserReturn>      ReturnValues        = new List<ParserReturn>();
            #endregion

            #region calibration
            //resets the statemachine
            Reset();

            //point to the first token
            NextToken();
            #endregion

            #region iterate tokenStream
            while (true)
            {
            #region Wohoo, we are in place
            //Todo: handle literals
            if (_currentToken.Terminal.Name.Equals("EOF"))
            {
                while (ShouldBeReduced(_currentState.Actions))
                {
                    Reduce();
                }

                foreach (KeyValuePair<string, ActionRecord> aAction in _currentState.Actions)
                {
                    aActionKey = aAction.Key;
                    aActionValue = aAction.Value;

                    if (!aActionKey.EndsWith("\b"))
                    {
                        //Try to find terminals
                        PossibleTerminals.Add(aActionKey);
                    }
                    else
                    {
                        //so... maybe it is a literal or a nonterminal that should be shown during autocompletion?
                        foreach (LRItem aActionShiftItem in aActionValue.ShiftItems)
                        {
                            NonTerminal _aNonTerminal = aActionShiftItem.Core.Current as NonTerminal;

                            if (_aNonTerminal != null)
                            {
                                if (_aNonTerminal.IsUsedForAutocompletion)
                                {
                                    PossibleTerminals.Add(_aNonTerminal.Description);

                                    if (!_GetAllTerminals)
                                    {
                                        foreach (String aTerminal in _aNonTerminal.Firsts)
                                        {
                                            RedundantTerminals.Add(aTerminal);
                                        }
                                    }

                                    break;
                                }

                            }//NonTerminal ?
                            else
                            {
                                aActionType = aActionShiftItem.Core.Current.GetType().Name;
                                if (aActionType.Contains("Literal"))
                                {
                                    PossibleTerminals.Add(aActionKey);
                                    break;
                                }
                            }//Literal !
                        }
                    }//else
                }//foreach

                break;
            }
            #endregion

            #region shifting
            //Get action
            action = GetCurrentAction();

            if (action == null)
            {
                //do sth else
            }//action==null

            if (action.HasConflict())
                action = (ActionRecord)Data.Grammar.OnActionConflict(this, _currentToken, action);
            this.OnActionSelected(_currentToken, action);

            switch (action.ActionType)
            {
                case ParserActionType.Operator:
                    if (GetActionTypeForOperation(_currentToken) == ParserActionType.Shift)
                        goto case ParserActionType.Shift;
                    else
                        goto case ParserActionType.Reduce;

                case ParserActionType.Shift:
                    ExecuteShiftAction(action);
                    break;

                case ParserActionType.Reduce:
                    ExecuteReduceAction(action);
                    break;
            }//switch

            #endregion
            }//while
            #endregion

            //Delete redundant Terminals (that have a description)
            foreach (String aRedundantElement in RedundantTerminals)
            {
            if (PossibleTerminals.Contains(aRedundantElement))
            {
                PossibleTerminals.Remove(aRedundantElement);
            }
            }

            #region create real completion info

            foreach (String aPossibleTerminal in PossibleTerminals)
            {
            String description = "";
            String name;
            Boolean isUsedForAutocompletion;
            Type typeOfLiteral = null;
            Boolean foundSth = false;

            if (aPossibleTerminal.EndsWith("\b"))
            {
                int endIndex = aPossibleTerminal.Length;
                name = aPossibleTerminal.Substring(0, endIndex - 1);
                isUsedForAutocompletion = true;
            }
            else
            {
                name = aPossibleTerminal;
                isUsedForAutocompletion = false;
            }

            int terminalIndex = 0;
            foreach (Terminal aTerminal in _context.Compiler.Grammar.Terminals)
            {
                if (aTerminal.Name.Equals(name))
                {
                    foundSth = true;
                    break;
                }
                else
                {
                    terminalIndex++;
                }
            }

            if (foundSth)
            {
                description = _context.Compiler.Grammar.Terminals[terminalIndex].Description;
                typeOfLiteral = _context.Compiler.Grammar.Terminals[terminalIndex].GetType();
            }

            ParserReturn aParserReturn = new ParserReturn(name, description, typeOfLiteral, isUsedForAutocompletion, true);

            ReturnValues.Add(aParserReturn);
            }

            #endregion

            return ReturnValues;
        }
Example #4
0
        public List<ParserReturn> GetPossibleTokens(CompilerContext MyCompilerContext, List<List<Token>> tokenStream, String InputString)
        {
            #region INPUT EXCEPTIONS

            if (MyCompilerContext == null || tokenStream == null || InputString == null)
            {
            throw new ArgumentNullException();
            }

            #endregion

            #region Data
            CompilerContext     _CompilerContext    = MyCompilerContext;
            String              _InputString        = InputString;
            List<Token>         FilteredTokenStream = null;
            String              ToBeCompleted       = "";
            Boolean             GetAllTerminals     = true;
            List<ParserReturn>  PossibleTerminalsTemp;
            List<ParserReturn>  PossibleTerminalsResult = new List<ParserReturn>();
            #endregion

            #region collect possible terminals

            //reasonableness of tokenStream
            FilteredTokenStream = CheckPlausibility(MyCompilerContext, tokenStream);

            //get the string that has to be completed
            ToBeCompleted = GetLastWord(FilteredTokenStream, _InputString);

            if (ToBeCompleted.Length.Equals(0)) GetAllTerminals = false;

            //collect all the possible options corresponding to the current state
            PossibleTerminalsTemp = GetPossibleTerminals(MyCompilerContext, FilteredTokenStream, GetAllTerminals);

            #endregion

            #region match terminals with input

            //get all matching Terminals that start with the InputString
            foreach (ParserReturn _aReturnValue in PossibleTerminalsTemp)
            {
            if (_aReturnValue.name.ToLower().StartsWith(ToBeCompleted.ToLower()))
            {
                PossibleTerminalsResult.Add(_aReturnValue);
            }
            }

            #endregion

            if (PossibleTerminalsResult.Count.Equals(1))
            {

            #region consider the case of only one possible token

            #region new Name
            String Option = PossibleTerminalsResult[0].name.Split(' ')[0].ToLower();
            int CountChars;
            int FirstNode;

            CountChars = Option.Length - ToBeCompleted.Length;
            FirstNode = ToBeCompleted.Length;

            String OptionSubstring = Option.Substring(FirstNode, CountChars);
            #endregion

            #region Create new Return Value

            ParserReturn newReturnValue = new ParserReturn(OptionSubstring, PossibleTerminalsResult[0].description, PossibleTerminalsResult[0].typeOfLiteral, PossibleTerminalsResult[0].isUsedForAutoCompletion, false);

            #endregion

            PossibleTerminalsResult.RemoveAt(0);
            PossibleTerminalsResult.Add(newReturnValue);

            }

            #endregion

            return PossibleTerminalsResult;
        }
Example #5
0
        private AcInformation UseAutocompletion(ParserReturn AcMetadata)
        {
            #region Data

            String toBeACed = "";
            String InputStringSubstringCursorPrefix = InputString.Substring(0, currentCursorPosWithinCommand);

            #endregion

            //get the already typed Literal, that has to be autocompleted
            int lastIndexOfDelimitter = InputStringSubstringCursorPrefix.LastIndexOf('\'');
            toBeACed = InputStringSubstringCursorPrefix.Substring(lastIndexOfDelimitter).Replace("'", "");

            if (AutoCompletions.ContainsKey(AcMetadata.name.ToLower()))
            {
                return new AcInformation(AutoCompletions[AcMetadata.name.ToLower()].Complete(_GraphDSSharp, ref CurrentPath, toBeACed), AcMetadata, toBeACed, lastIndexOfDelimitter);

            }//is this AC available?
            else
            {
                WriteLine(Environment.NewLine);

                WriteLine("Warning! Sorry, but the \"{0}\" is currently not available!{1}Please type this one manually.", AcMetadata.name, Environment.NewLine);

                WriteLine(Environment.NewLine);

                WritePromptToConsole(default_color, CurrentPath);

                Write(InputString);

                return null;
            }
        }