Esempio n. 1
0
 /// <summary>
 /// Constructs a new instance of the
 /// <see cref="PredicateEvalInfo"/>
 /// class with the
 /// specified detailed predicate evaluation information.
 /// </summary>
 /// <param name="state">The simulator state</param>
 /// <param name="decision">The decision number</param>
 /// <param name="input">The input token stream</param>
 /// <param name="startIndex">The start index for the current prediction</param>
 /// <param name="stopIndex">
 /// The index at which the predicate evaluation was
 /// triggered. Note that the input stream may be reset to other positions for
 /// the actual evaluation of individual predicates.
 /// </param>
 /// <param name="semctx">The semantic context which was evaluated</param>
 /// <param name="evalResult">The results of evaluating the semantic context</param>
 /// <param name="predictedAlt">
 /// The alternative number for the decision which is
 /// guarded by the semantic context
 /// <paramref name="semctx"/>
 /// . See
 /// <see cref="predictedAlt"/>
 /// for more information.
 /// </param>
 /// <seealso cref="ParserATNSimulator.EvalSemanticContext(SemanticContext, ParserRuleContext, int)"/>
 /// <seealso cref="SemanticContext.Eval"/>
 public PredicateEvalInfo(SimulatorState state, int decision, ITokenStream input, int startIndex, int stopIndex, SemanticContext semctx, bool evalResult, int predictedAlt)
     : base(decision, state, input, startIndex, stopIndex, state.useContext)
 {
     this.semctx = semctx;
     this.evalResult = evalResult;
     this.predictedAlt = predictedAlt;
 }
Esempio n. 2
0
 public SymbolList(SemanticContext semanticContext)
 {
     _Symbols        = new List <SymbolItem>();
     FilteredItems   = new ListCollectionView(_Symbols);
     SemanticContext = semanticContext;
     _SymbolTip      = new ToolTip {
         Placement       = System.Windows.Controls.Primitives.PlacementMode.Bottom,
         PlacementTarget = this
     };
     Resources = SharedDictionaryManager.SymbolList;
 }
Esempio n. 3
0
        public CSharpSmartBar(IWpfTextView view, Microsoft.VisualStudio.Text.Operations.ITextSearchService2 textSearchService) : base(view, textSearchService)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            _Context                         = view.Properties.GetOrCreateSingletonProperty(() => new SemanticContext(view));
            _SymbolListContainer             = new ExternalAdornment(view);
            View.Selection.SelectionChanged += ViewSeletionChanged;
            var extenders = CodistPackage.DTE.ActiveDocument?.ProjectItem?.ContainingProject?.ExtenderNames as string[];

            if (extenders != null)
            {
                _IsVsProject = Array.IndexOf(extenders, "VsixProjectExtender") != -1;
            }
        }
Esempio n. 4
0
        public TypeCheckVisitor(SemanticContext context, TypeManager typeManager, IErrorManager errors, ExpressionTypeManager expressionTypeManager)
        {
            _context               = context;
            _typeManager           = typeManager;
            _errors                = errors;
            _expressionTypeManager = expressionTypeManager;

            _delegator.StatementVisitor  = this;
            _delegator.ExpressionVisitor = this;
            _delegator.SpecifierVisitor  = this;

            // TODO: Better way to get this void type.
            Type = _typeManager.GetType("void", PointerMode.NotAPointer) !;
        }
Esempio n. 5
0
        protected void DefineFuncSymbol(ParseForest forest, SemanticContext context)
        {
            new ParseTreeVisitor(forest).Left().All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.HV:
                case NonTerminalSymbol.V:
                case NonTerminalSymbol.Adv:
                    context.SignatureBeingDefined.verb += (context.SignatureBeingDefined.verb == "") ? forest.a[i] : " " + forest.a[i];
                    return;
                }
            });
            "MULTIMETHOD ".Log(context.SignatureBeingDefined.verb);

            // parameter list
            Scope.EnterNew(context.SignatureBeingDefined.verb);

            var    resetVisitor = new ParseTreeVisitor(forest).Left();
            string errors       = WithAlternates(forest, resetVisitor, context, context.SignatureBeingDefined.verb);

            if (errors != null)
            {
                throw new Exception(" Nothing could possibly be passed into the higher-order function " + context.SignatureBeingDefined + Environment.NewLine + errors);
            }

            "SIGNATURE:".Log(context.SignatureBeingDefined);
            context.FunctionBeingDefined = new Symbol
            {
                Name         = context.SignatureBeingDefined.ToString(),
                Type         = context.SignatureBeingDefined.verb,
                Category     = Categories.FunctionMultimethod,
                PartOfSpeech = NonTerminalSymbol.V,
                Signature    = context.SignatureBeingDefined,
            };

            // define function in parent scope
            Scope.Current.Enclosing.Define(context.FunctionBeingDefined);

            // body of function
            "BODY".Log();
            context.InvocationBeingDefined = new Invocation("");
            CreateSymbol(forest, new ParseTreeVisitor(forest).Right(), context);
            AppendToFunctionBody(context);
            // body may continue if next sentence is imperative.
        }
Esempio n. 6
0
        internal static void FindOverrides(this SemanticContext context, ISymbol symbol)
        {
            var m = new SymbolMenu(context);
            int c = 0;

            foreach (var ov in SyncHelper.RunSync(() => SymbolFinder.FindOverridesAsync(symbol, context.Document.Project.Solution, null, default)))
            {
                m.Menu.Add(ov, ov.ContainingType);
                ++c;
            }
            m.Title.SetGlyph(ThemeHelper.GetImage(symbol.GetImageId()))
            .Append(symbol.ToDisplayString(CodeAnalysisHelper.MemberNameFormat), true)
            .Append(R.T_Overrides)
            .Append(c.ToString());
            m.Show();
        }
Esempio n. 7
0
#pragma warning restore 649, 169

        public void TextViewCreated(IWpfTextView textView)
        {
            if (Config.Instance.Features.MatchFlags(Features.NaviBar) &&
                textView.Roles.Contains("DIFF") == false)
            {
                if (textView.TextBuffer.ContentType.IsOfType(Constants.CodeTypes.CSharp) || textView.TextBuffer.LikeContentType(Constants.CodeTypes.Markdown))
                {
                    SemanticContext.GetOrCreateSingetonInstance(textView);
                    new Overrider(textView, _TextSearchService);
                }
#if DEBUG
                else
                {
                    AssociateFileCodeModelOverrider();
                }
#endif
            }
        }
        public void Process(StructNode structNode, SemanticContext context, IErrorManager errors)
        {
            for (int i = 0, ilen = structNode.FunctionAssociations.Count; i < ilen; ++i)
            {
                IStructFunctionAssociationNode associationNode = structNode.FunctionAssociations[i];

                if (associationNode is StructFunctionAssociationParseNode parseNode)
                {
                    if (!context.Functions.TryGetValue(parseNode.FunctionName, out FunctionDefinitionNode function))
                    {
                        errors.AddError($"Undefined function \"{parseNode.FunctionName}\"", parseNode);
                        continue;
                    }

                    structNode.FunctionAssociations[i] = new StructFunctionAssociationNode(parseNode.Name, function);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Constructor for the <see cref="CSharpMembersMargin"/>.
        /// </summary>
        /// <param name="textView">ITextView to which this <see cref="CSharpMembersMargin"/> will be attacheded.</param>
        /// <param name="verticalScrollbar">Vertical scrollbar of the ITextViewHost that contains <paramref name="textView"/>.</param>
        public CSharpMembersMargin(IWpfTextView textView, IVerticalScrollBar verticalScrollbar)
        {
            IsHitTestVisible    = false;
            SnapsToDevicePixels = true;
            Width                  = Padding + MarkerSize;
            _MemberMarker          = new MemberMarker(textView, verticalScrollbar, this);
            _SymbolReferenceMarker = new SymbolReferenceMarker(textView, verticalScrollbar, this);
            _FormatMap             = ServicesHelper.Instance.EditorFormatMap.GetEditorFormatMap(textView);
            _SemanticContext       = SemanticContext.GetOrCreateSingetonInstance(textView);
            IsVisibleChanged      += _MemberMarker.OnIsVisibleChanged;
            textView.Closed       += TextView_Closed;

            Config.Updated += Config_Updated;
            Config_Updated(null, new ConfigUpdatedEventArgs(Features.ScrollbarMarkers));
            if (Config.Instance.MarkerOptions.MatchFlags(MarkerOptions.SymbolReference))
            {
                _SymbolReferenceMarker.HookEvents();
            }
        }
Esempio n. 10
0
 public FunctionCodeGenVisitor(
     FunctionBuilder functionBuilder,
     FunctionStackLayout layout,
     FunctionManager functionManager,
     SemanticModule module,
     SemanticContext semanticContext,
     StructLayoutManager structLayoutManager,
     ILTypeSizeManager typeSizeManager,
     List <string> strings
     )
 {
     _functionBuilder     = functionBuilder;
     _layout              = layout;
     _functionManager     = functionManager;
     _module              = module;
     _semanticContext     = semanticContext;
     _structLayoutManager = structLayoutManager;
     _typeSizeManager     = typeSizeManager;
     _strings             = strings;
 }
Esempio n. 11
0
        public CodeGeneratorContext(Context context, SemanticModule semanticModule, SemanticContext semanticContext,
                                    string fileName, string dirName, string targetTriple, bool optimized, bool debugInfo, bool columnInfo)
        {
            Context         = context;
            SemanticModule  = semanticModule;
            SemanticContext = semanticContext;

            Module = context.CreateModule(fileName);
            Module.SetTarget(targetTriple);

            // Debug compile unit is always emitted even if debug info is not requested
            // This provides a standardized way to export enums in the module
            Module.AddModuleFlag(LLVMModuleFlagBehavior.LLVMModuleFlagBehaviorWarning, "Debug Info Version",
                                 Context.DebugMetadataVersion);
            if (targetTriple.Contains("-msvc"))
            {
                Module.AddModuleFlag(LLVMModuleFlagBehavior.LLVMModuleFlagBehaviorWarning, "CodeView",
                                     Metadata.FromValue(Value.ConstInt(Context.Int32Type, 1, false)));
            }

            DiFile = DiBuilder.CreateFile(fileName, dirName);
            // Just say that MonC is C89; hopefully debuggers won't care
            Metadata diCompileUnit = DiBuilder.CreateCompileUnit(LLVMDWARFSourceLanguage.LLVMDWARFSourceLanguageC89,
                                                                 DiFile, "MonC", optimized, "", 0, "", LLVMDWARFEmissionKind.LLVMDWARFEmissionFull, 0, false, false);

            DiModule = DiBuilder.CreateModule(diCompileUnit, fileName, "", "", "");

            DebugInfo  = debugInfo;
            ColumnInfo = columnInfo && debugInfo;

            // IR-independent manager to generate struct type layouts
            StructLayoutManager.Setup(new StructLayoutGenerator(new IndexTypeSizeManager()));

            // Struct sizes need to be resolved for debug info (which is target-dependent)
            Target        target  = Target.FromTriple(targetTriple);
            TargetMachine machine = target.CreateTargetMachine(targetTriple, "", "",
                                                               LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault, LLVMRelocMode.LLVMRelocDefault,
                                                               LLVMCodeModel.LLVMCodeModelDefault);

            TargetDataLayout = machine.CreateTargetDataLayout();
        }
Esempio n. 12
0
        protected void DefineRelationVerb(ParseForest forest, SemanticContext context)
        {
            new ParseTreeVisitor(forest).All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.V:
                case NonTerminalSymbol.HV:
                //case NonTerminalSymbol.G:
                case NonTerminalSymbol.Adv:
                    context.SignatureBeingDefined.verb += (context.SignatureBeingDefined.verb == "") ? forest.a[i] : " " + forest.a[i];
                    return;
                }
            });
            if (string.IsNullOrWhiteSpace(context.SignatureBeingDefined.verb))
            {
                PrintAST(forest);
                throw new Exception("Can't find the main verb.");
            }
            "MULTIMETHOD_R:".Log(context.SignatureBeingDefined.verb);

            // parameter list
            using (Scope.EnterNew(context.SignatureBeingDefined.verb))
                CreateSymbol(forest, new ParseTreeVisitor(forest), context);
            "SIGNATURE:".Log(context.SignatureBeingDefined);

            Scope.Current.Define(new Symbol
            {
                Name         = context.SignatureBeingDefined.ToString(),
                Type         = context.SignatureBeingDefined.verb,
                Category     = Categories.ClassObjectStructType,             // well, struct if no exe body, FuncMultimethod if exe body
                PartOfSpeech = NonTerminalSymbol.V,
                Signature    = context.SignatureBeingDefined,
            });
        }
Esempio n. 13
0
#pragma warning restore 649, 169

        /// <summary>
        /// Instantiates a SelectionBar when a textView is created.
        /// </summary>
        /// <param name="textView">The <see cref="IWpfTextView"/> upon which the adornment should be placed</param>
        public void TextViewCreated(IWpfTextView textView)
        {
            if (Config.Instance.Features.MatchFlags(Features.SmartBar) == false)
            {
                return;
            }
            textView.VisualElement.Loaded += TextViewLoaded;
            // The toolbar will get wired to the text view events
            var contentType = textView.TextBuffer.ContentType;

            if (Constants.CodeTypes.CSharp.Equals(contentType.TypeName, StringComparison.OrdinalIgnoreCase))
            {
                SemanticContext.GetOrCreateSingetonInstance(textView);
                new CSharpSmartBar(textView, _TextSearchService);
            }
            else if (textView.TextBuffer.LikeContentType(Constants.CodeTypes.Markdown))
            {
                new MarkdownSmartBar(textView, _TextSearchService);
            }
            else if (contentType.IsOfType("output") ||
                     contentType.IsOfType("FindResults") ||
                     contentType.IsOfType("Interactive Content") ||
                     contentType.IsOfType("DebugOutput") ||
                     contentType.IsOfType("Command") ||
                     contentType.IsOfType("PackageConsole")
                     )
            {
                new OutputSmartBar(textView, _TextSearchService);
            }
            else if (contentType.IsOfType(Constants.CodeTypes.CPlusPlus))
            {
                new CppSmartBar(textView, _TextSearchService);
            }
            else
            {
                new SmartBar(textView, _TextSearchService);
            }
        }
Esempio n. 14
0
        protected void DefineTemporalConstraint(ParseForest forest, SemanticContext context)
        {
            var DCvisitor = new ParseTreeVisitor(forest);
            var MCvisitor = new ParseTreeVisitor(DCvisitor);

            if (DCvisitor.currentNode.B == NonTerminalSymbol.DC)
            {
                DCvisitor.Left(); MCvisitor.Right();
            }
            else
            {
                DCvisitor.Right(); MCvisitor.Left();
            }

            context.SignatureBeingDefined.verb = "??constraint";
            context.InvocationBeingDefined     = new Invocation("");

            context.FunctionBeingDefined = new Symbol
            {
                Category     = Categories.Constraint,
                PartOfSpeech = NonTerminalSymbol.TR,
            };
            Scope.EnterNew(context.SignatureBeingDefined.verb);
            DefineImpInvSymbol(forest, context, MCvisitor);
            "  MAIN CLAUSE:".Log(context.SignatureBeingDefined.verb);

            DefineImpInvSymbol(forest, context, DCvisitor);
            "  DEPENDENT CLAUSE:".Log(context.SignatureBeingDefined.verb);

            "DEFINED constraint:".Log(context.SignatureBeingDefined);

            context.FunctionBeingDefined.Name      = context.SignatureBeingDefined.ToString();
            context.FunctionBeingDefined.Type      = context.SignatureBeingDefined.verb;
            context.FunctionBeingDefined.Signature = context.SignatureBeingDefined;
            Scope.Current.Define(context.FunctionBeingDefined);
            EndPreviousFunctionBody(context);
        }
Esempio n. 15
0
 internal override void SemanticValidation(SemanticContext semanticContext)
 {
     /*bool multiMatch =  semanticContext.FunctionDeclarations.Where(f =>
      * {
      *  bool match = true;
      *  match = f.Id == this.Id && f.Parameters.Count == this.Parameters.Count;
      *  if (match)
      *  {
      *      for (int i = 0; i < f.Parameters.Count; i++)
      *      {
      *          match = f.Parameters[i].Type == this.Parameters[i].Type;
      *          if (!match)
      *              break;
      *      }
      *  }
      *  return match;
      * }).Count() == 1;
      *
      * if (!multiMatch)
      * {
      *  throw new SemanticValidationException("Ambiguous Type of Function Definition: " + id);
      * }
      * Block.SemanticValidation(semanticContext);*/
 }
Esempio n. 16
0
 public CSharpBar(IWpfTextView textView)
 {
     _View            = textView;
     _Adornment       = _View.GetAdornmentLayer(nameof(CSharpBar));
     _SemanticContext = textView.Properties.GetOrCreateSingletonProperty(() => new SemanticContext(textView));
     this.SetBackgroundForCrispImage(ThemeHelper.TitleBackgroundColor);
     textView.Properties.AddProperty(nameof(NaviBar), this);
     Name      = nameof(CSharpBar);
     Resources = SharedDictionaryManager.Menu;
     SetResourceReference(BackgroundProperty, VsBrushes.CommandBarMenuBackgroundGradientKey);
     SetResourceReference(ForegroundProperty, VsBrushes.CommandBarTextInactiveKey);
     Items.Add(_RootItem = new RootItem(this));
     _View.Selection.SelectionChanged += Update;
     _View.Closed += ViewClosed;
     Update(this, EventArgs.Empty);
     foreach (var m in _SemanticContext.Compilation.Members)
     {
         if (m.IsKind(SyntaxKind.NamespaceDeclaration))
         {
             _RootItem.SetText(m.GetDeclarationSignature());
             break;
         }
     }
 }
Esempio n. 17
0
        internal static void FindMembers(this SemanticContext context, ISymbol symbol, UIElement positionElement = null)
        {
            var m = new SymbolMenu(context, symbol.Kind == SymbolKind.Namespace ? SymbolListType.TypeList : SymbolListType.None);

            var(count, external) = m.Menu.AddSymbolMembers(symbol);
            if (m.Menu.IconProvider == null)
            {
                if (symbol.Kind == SymbolKind.NamedType)
                {
                    switch (((INamedTypeSymbol)symbol).TypeKind)
                    {
                    case TypeKind.Interface:
                        m.Menu.ExtIconProvider = ExtIconProvider.InterfaceMembers.GetExtIcons; break;

                    case TypeKind.Class:
                    case TypeKind.Struct:
                        m.Menu.ExtIconProvider = ExtIconProvider.Default.GetExtIcons; break;
                    }
                }
                else
                {
                    m.Menu.ExtIconProvider = ExtIconProvider.Default.GetExtIcons;
                }
            }
            m.Title.SetGlyph(ThemeHelper.GetImage(symbol.GetImageId()))
            .Append(symbol.ToDisplayString(CodeAnalysisHelper.MemberNameFormat), true);
            if (symbol.Kind != SymbolKind.Namespace)
            {
                m.Title.Append(R.T_Members.Replace("{count}", count.ToString()).Replace("{inherited}", external.ToString()));
            }
            else
            {
                m.Title.Append(R.T_NamespaceMembers.Replace("{count}", count.ToString()));
            }
            m.Show(positionElement);
        }
Esempio n. 18
0
 internal Module CreateModule(FileInfo fileInfo, SemanticModule semanticModule,
                              SemanticContext semanticContext) =>
 CodeGenerator.Generate(_context, fileInfo.FullPath, semanticModule, semanticContext, _targetTriple,
                        _optBuilder, _debugInfo, _debugColumnInfo);
Esempio n. 19
0
 private TypeInference(SemanticContext context)
 {
     _context = context;
 }
Esempio n. 20
0
        static void ApplyClickAndGo(ISymbol symbol, ITextBuffer textBuffer, TextBlock description, IAsyncQuickInfoSession quickInfoSession)
        {
            if (symbol.Kind == SymbolKind.Namespace)
            {
                description.ToolTip     = R.T_Locations + symbol.DeclaringSyntaxReferences.Length;
                description.MouseEnter += HookEvents;
                return;
            }
            if (symbol.Kind == SymbolKind.Method)
            {
                if (((IMethodSymbol)symbol).MethodKind == MethodKind.LambdaMethod)
                {
                    using (var sbr = Microsoft.VisualStudio.Utilities.ReusableStringBuilder.AcquireDefault(30)) {
                        var sb = sbr.Resource;
                        sb.Append('(');
                        foreach (var item in ((IMethodSymbol)symbol).Parameters)
                        {
                            if (item.Ordinal > 0)
                            {
                                sb.Append(", ");
                            }
                            sb.Append(item.Type.ToDisplayString(CodeAnalysisHelper.QuickInfoSymbolDisplayFormat))
                            .Append(item.Type.GetParameterString())
                            .Append(' ')
                            .Append(item.Name);
                        }
                        sb.Append(')');
                        description.Append(sb.ToString(), ThemeHelper.DocumentTextBrush);
                    }
                }
            }
            description.UseDummyToolTip();
            if (symbol.HasSource() == false && symbol.ContainingType?.HasSource() == true)
            {
                // if the symbol is implicitly declared but its containing type is in source,
                // navigate to the containing type
                symbol = symbol.ContainingType;
            }
            description.MouseEnter += HookEvents;

            void HookEvents(object sender, MouseEventArgs e)
            {
                var s = sender as FrameworkElement;

                s.MouseEnter -= HookEvents;
                HighlightSymbol(sender, e);
                s.Cursor = Cursors.Hand;
                if (symbol.Kind != SymbolKind.Namespace)
                {
                    s.ToolTipOpening += ShowToolTip;
                    s.UseDummyToolTip();
                }
                s.MouseEnter         += HighlightSymbol;
                s.MouseLeave         += RemoveSymbolHighlight;
                s.MouseLeftButtonUp  += GoToSource;
                s.ContextMenuOpening += ShowContextMenu;
                s.ContextMenuClosing += ReleaseQuickInfo;
            }

            async void GoToSource(object sender, MouseButtonEventArgs e)
            {
                await quickInfoSession.DismissAsync();

                symbol.GoToDefinition();
            }

            void ShowToolTip(object sender, ToolTipEventArgs e)
            {
                var t = sender as TextBlock;

                t.ToolTip         = ShowSymbolLocation(symbol, symbol.HasSource() ? System.IO.Path.GetFileName(symbol.Locations[0].SourceTree.FilePath) : symbol.GetAssemblyModuleName());
                t.ToolTipOpening -= ShowToolTip;
            }

            void HighlightSymbol(object sender, EventArgs e)
            {
                ((TextBlock)sender).Background = (symbol.HasSource() ? SystemColors.HighlightBrush : SystemColors.GrayTextBrush).Alpha(0.3);
            }

            void RemoveSymbolHighlight(object sender, MouseEventArgs e)
            {
                ((TextBlock)sender).Background = Brushes.Transparent;
            }

            void ShowContextMenu(object sender, ContextMenuEventArgs e)
            {
                var s = sender as FrameworkElement;

                if (s.ContextMenu == null)
                {
                    var ctx = SemanticContext.GetOrCreateSingetonInstance(quickInfoSession.TextView as IWpfTextView);
                    SyncHelper.RunSync(() => ctx.UpdateAsync(textBuffer, default));
                    var m = new CSharpSymbolContextMenu(ctx)
                    {
                        Symbol     = symbol,
                        SyntaxNode = symbol.GetSyntaxNode()
                    };
                    m.AddAnalysisCommands();
                    if (m.HasItems)
                    {
                        m.Items.Add(new Separator());
                    }
                    m.AddSymbolNodeCommands();
                    m.AddTitleItem(symbol.GetOriginalName());
                    m.ItemClicked += HideQuickInfo;
                    s.ContextMenu  = m;
                }
                HoldQuickInfo(s, true);
                s.ContextMenu.IsOpen = true;
            }

            void ReleaseQuickInfo(object sender, ContextMenuEventArgs e)
            {
                HoldQuickInfo(sender as DependencyObject, false);
            }

            void HideQuickInfo(object sender, RoutedEventArgs e)
            {
                DismissQuickInfo(description);
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Create or modify a lexeme with no word form basis, gets tricky with best fit scenarios
        /// </summary>
        /// <param name="word">just the text of the word</param>
        /// <returns>A lexeme</returns>
        public static ILexeme CreateOrModifyLexeme(ILanguage language, string word, LexicalType wordType, ref List <string> processedWords)
        {
            word = word.ToLower();

            Regex rgx = new Regex("[^a-z -]");

            if (rgx.IsMatch(word))
            {
                return(null);
            }

            ILexeme newLex = ConfigDataCache.Get <ILexeme>(string.Format("{0}_{1}_{2}", ConfigDataType.Dictionary, language.Name, word));

            if (newLex == null)
            {
                newLex = language.CreateOrModifyLexeme(word, wordType, new string[0]);
            }

            if ((newLex.IsSynMapped && newLex.MirriamIndexed) || processedWords.Any(wrd => wrd.Equals(word)))
            {
                if (!processedWords.Any(wrd => wrd.Equals(word)))
                {
                    processedWords.Add(word);
                }
            }
            else
            {
                LexicalType[] invalidTypes = new LexicalType[] { LexicalType.Article, LexicalType.Conjunction, LexicalType.ProperNoun, LexicalType.Pronoun, LexicalType.None };

                processedWords.Add(word);

                //This is wordnet processing, wordnet doesnt have any of the above and will return weird results if we let it
                if (!invalidTypes.Contains(wordType))
                {
                    var synSets = WordNetHarness.GetSynSets(word, new PartOfSpeech[] { PartOfSpeech.Adjective, PartOfSpeech.Adverb, PartOfSpeech.Noun, PartOfSpeech.Verb });

                    //We in theory have every single word form for this word now
                    if (synSets != null)
                    {
                        SemanticContext[] invalidContexts = new SemanticContext[]
                        { SemanticContext.Group, SemanticContext.Event, SemanticContext.Location, SemanticContext.Competition, SemanticContext.Person
                          , SemanticContext.Plant, SemanticContext.Animal, SemanticContext.Time, SemanticContext.Artifact };

                        foreach (SynSet synSet in synSets)
                        {
                            if (synSet.PartOfSpeech == PartOfSpeech.None)
                            {
                                continue;
                            }

                            var synContext = TranslateContext(synSet.LexicographerFileName);

                            if (invalidContexts.Contains(synContext))
                            {
                                continue;
                            }

                            var newDict = newLex.GetForm(MapLexicalTypes(synSet.PartOfSpeech), -1);

                            if (newDict == null)
                            {
                                newLex          = language.CreateOrModifyLexeme(word, MapLexicalTypes(synSet.PartOfSpeech), new string[0]);
                                newDict         = newLex.GetForm(MapLexicalTypes(synSet.PartOfSpeech), -1);
                                newDict.Context = TranslateContext(synSet.LexicographerFileName);
                            }

                            //We're going to use the definition from here
                            if (!string.IsNullOrWhiteSpace(synSet.Gloss))
                            {
                                newDict.Definition = synSet.Gloss;
                            }

                            ///wsns indicates hypo/hypernymity so
                            foreach (string synWord in synSet.Words)
                            {
                                var newWord = synWord.ToLower();
                                newWord = newWord.Replace("_", " ");

                                if (rgx.IsMatch(newWord))
                                {
                                    continue;
                                }

                                int myElegance = Math.Max(0, newWord.SyllableCount() * 3);

                                processedWords.Add(newWord);

                                if (string.IsNullOrWhiteSpace(newWord) || newWord.All(ch => ch == '-') || newWord.IsNumeric())
                                {
                                    continue;
                                }

                                var synLex = language.CreateOrModifyLexeme(newWord, MapLexicalTypes(synSet.PartOfSpeech), newDict.Semantics.ToArray());

                                var synDict = synLex.GetForm(MapLexicalTypes(synSet.PartOfSpeech), newDict.Semantics.ToArray(), false);
                                synDict.Elegance   = 0;
                                synDict.Quality    = 0;
                                synDict.Severity   = 0;
                                synDict.Context    = synContext;
                                synDict.Definition = newDict.Definition;

                                synLex.PersistToCache();
                                synLex.SystemSave();

                                if (!newWord.Equals(word))
                                {
                                    newDict.MakeRelatedWord(language, newWord, true, synDict);
                                }
                            }
                        }
                    }
                }

                newLex.IsSynMapped = true;
                newLex.SystemSave();
                newLex.PersistToCache();
            }

            if (!newLex.MirriamIndexed)
            {
                var newDict = newLex.GetForm(0);

                try
                {
                    var dictEntry = MirriamWebsterAPI.GetDictionaryEntry(newLex.Name);
                    if (dictEntry != null)
                    {
                        //Stuff done to modify all forms of the lexeme
                        foreach (var dict in newLex.WordForms)
                        {
                            dict.Vulgar = dictEntry.meta.offensive;
                        }

                        //Stuff done based on the dictionary return data
                        foreach (var stemWord in dictEntry.uros)
                        {
                            if (newLex.GetForm(MapLexicalTypes(stemWord.fl)) == null)
                            {
                                var     wordText = stemWord.ure.Replace("*", "");
                                ILexeme stemLex  = ConfigDataCache.Get <ILexeme>(string.Format("{0}_{1}_{2}", ConfigDataType.Dictionary, language.Name, wordText));

                                if (stemLex == null)
                                {
                                    stemLex = language.CreateOrModifyLexeme(wordText, MapLexicalTypes(stemWord.fl), null);

                                    var stemDict = stemLex.GetForm(0);
                                    stemDict.Elegance   = newDict.Elegance;
                                    stemDict.Quality    = newDict.Quality;
                                    stemDict.Severity   = newDict.Severity;
                                    stemDict.Context    = newDict.Context;
                                    stemDict.Definition = newDict.Definition;
                                    stemDict.Semantics  = newDict.Semantics;
                                    processedWords.Add(wordText);

                                    stemLex.SystemSave();
                                    stemLex.PersistToCache();
                                }
                            }
                        }

                        newDict.Semantics = new HashSet <string>(dictEntry.sls);
                    }
                }
                catch
                {
                    //just eating it
                }

                try
                {
                    var thesEntry = MirriamWebsterAPI.GetThesaurusEntry(newLex.Name);
                    if (thesEntry != null)
                    {
                        foreach (var synonym in thesEntry.meta.syns.SelectMany(syn => syn))
                        {
                            var newWord = synonym.ToLower();
                            newWord = newWord.Replace("_", " ");

                            if (rgx.IsMatch(newWord) || string.IsNullOrWhiteSpace(newWord) || newWord.All(ch => ch == '-'))
                            {
                                continue;
                            }

                            var synLex = language.CreateOrModifyLexeme(newWord, MapLexicalTypes(thesEntry.fl), newDict.Semantics.ToArray());

                            var synDict = synLex.GetForm(MapLexicalTypes(thesEntry.fl), newDict.Semantics.ToArray(), false);
                            synDict.Elegance   = 0;
                            synDict.Quality    = 0;
                            synDict.Severity   = 0;
                            synDict.Context    = newDict.Context;
                            synDict.Definition = newDict.Definition;

                            synLex.PersistToCache();
                            synLex.SystemSave();
                            processedWords.Add(newWord);

                            if (!newWord.Equals(word))
                            {
                                newDict.MakeRelatedWord(language, newWord, true, synDict);
                            }
                        }

                        foreach (var antonym in thesEntry.meta.ants.SelectMany(syn => syn))
                        {
                            var newWord = antonym.ToLower();
                            newWord = newWord.Replace("_", " ");

                            if (rgx.IsMatch(newWord) || string.IsNullOrWhiteSpace(newWord) || newWord.All(ch => ch == '-'))
                            {
                                continue;
                            }

                            var synLex = language.CreateOrModifyLexeme(newWord, MapLexicalTypes(thesEntry.fl), newDict.Semantics.ToArray());

                            var synDict = synLex.GetForm(MapLexicalTypes(thesEntry.fl), newDict.Semantics.ToArray(), false);
                            synDict.Elegance   = 0;
                            synDict.Quality    = 0;
                            synDict.Severity   = 0;
                            synDict.Context    = newDict.Context;
                            synDict.Definition = newDict.Definition;

                            synLex.PersistToCache();
                            synLex.SystemSave();
                            processedWords.Add(newWord);

                            if (!newWord.Equals(word))
                            {
                                newDict.MakeRelatedWord(language, newWord, false, synDict);
                            }
                        }
                    }
                }
                catch
                {
                    //just eating it
                }

                newLex.MirriamIndexed = true;
                newLex.SystemSave();
                newLex.PersistToCache();
            }

            if (!newLex.IsTranslated)
            {
            }

            return(newLex);
        }
Esempio n. 22
0
        protected virtual StringTemplate WalkFixedDFAGeneratingStateMachine(
            TemplateGroup templates,
            DFA dfa,
            DFAState s,
            int k)
        {
            //System.Console.Out.WriteLine( "walk " + s.stateNumber + " in dfa for decision " + dfa.decisionNumber );
            if (s.IsAcceptState)
            {
                StringTemplate dfaST2 = templates.GetInstanceOf("dfaAcceptState");
                dfaST2.SetAttribute("alt", s.GetUniquelyPredictedAlt());
                return(dfaST2);
            }

            // the default templates for generating a state and its edges
            // can be an if-then-else structure or a switch
            string dfaStateName              = "dfaState";
            string dfaLoopbackStateName      = "dfaLoopbackState";
            string dfaOptionalBlockStateName = "dfaOptionalBlockState";
            string dfaEdgeName = "dfaEdge";

            if (_parentGenerator.CanGenerateSwitch(s))
            {
                dfaStateName              = "dfaStateSwitch";
                dfaLoopbackStateName      = "dfaLoopbackStateSwitch";
                dfaOptionalBlockStateName = "dfaOptionalBlockStateSwitch";
                dfaEdgeName = "dfaEdgeSwitch";
            }

            StringTemplate dfaST = templates.GetInstanceOf(dfaStateName);

            if (dfa.NFADecisionStartState.decisionStateType == NFAState.LOOPBACK)
            {
                dfaST = templates.GetInstanceOf(dfaLoopbackStateName);
            }
            else if (dfa.NFADecisionStartState.decisionStateType == NFAState.OPTIONAL_BLOCK_START)
            {
                dfaST = templates.GetInstanceOf(dfaOptionalBlockStateName);
            }
            dfaST.SetAttribute("k", k);
            dfaST.SetAttribute("stateNumber", s.StateNumber);
            dfaST.SetAttribute("semPredState",
                               s.IsResolvedWithPredicates);

            /*
             * string description = dfa.getNFADecisionStartState().Description;
             * description = parentGenerator.target.getTargetStringLiteralFromString( description );
             * //System.Console.Out.WriteLine( "DFA: " + description + " associated with AST " + dfa.getNFADecisionStartState() );
             * if ( description != null )
             * {
             *  dfaST.SetAttribute( "description", description );
             * }
             */
            int      EOTPredicts = NFA.INVALID_ALT_NUMBER;
            DFAState EOTTarget   = null;

            //System.Console.Out.WriteLine( "DFA state " + s.stateNumber );
            for (int i = 0; i < s.NumberOfTransitions; i++)
            {
                Transition edge = (Transition)s.GetTransition(i);
                //System.Console.Out.WriteLine( "edge " + s.stateNumber + "-" + edge.label.ToString() + "->" + edge.target.stateNumber );
                if (edge.Label.Atom == Label.EOT)
                {
                    // don't generate a real edge for EOT; track alt EOT predicts
                    // generate that prediction in the else clause as default case
                    EOTTarget   = (DFAState)edge.Target;
                    EOTPredicts = EOTTarget.GetUniquelyPredictedAlt();

                    /*
                     * System.Console.Out.WriteLine("DFA s"+s.stateNumber+" EOT goes to s"+
                     *                 edge.target.stateNumber+" predicates alt "+
                     *                 EOTPredicts);
                     */
                    continue;
                }
                StringTemplate edgeST = templates.GetInstanceOf(dfaEdgeName);
                // If the template wants all the label values delineated, do that
                if (edgeST.impl.TryGetFormalArgument("labels") != null)
                {
                    List <string> labels = edge.Label.Set.Select(value => _parentGenerator.GetTokenTypeAsTargetLabel(value)).ToList();
                    edgeST.SetAttribute("labels", labels);
                }
                else
                { // else create an expression to evaluate (the general case)
                    edgeST.SetAttribute("labelExpr",
                                        _parentGenerator.GenLabelExpr(templates, edge, k));
                }

                // stick in any gated predicates for any edge if not already a pred
                if (!edge.Label.IsSemanticPredicate)
                {
                    DFAState        target = (DFAState)edge.Target;
                    SemanticContext preds  =
                        target.GetGatedPredicatesInNFAConfigurations();
                    if (preds != null)
                    {
                        //System.Console.Out.WriteLine( "preds=" + target.getGatedPredicatesInNFAConfigurations() );
                        StringTemplate predST = preds.GenExpr(_parentGenerator,
                                                              _parentGenerator.Templates,
                                                              dfa);
                        edgeST.SetAttribute("predicates", predST);
                    }
                }

                StringTemplate targetST =
                    WalkFixedDFAGeneratingStateMachine(templates,
                                                       dfa,
                                                       (DFAState)edge.Target,
                                                       k + 1);
                edgeST.SetAttribute("targetState", targetST);
                dfaST.SetAttribute("edges", edgeST);
                //System.Console.Out.WriteLine( "back to DFA " + dfa.decisionNumber + "." + s.stateNumber );
            }

            // HANDLE EOT EDGE
            if (EOTPredicts != NFA.INVALID_ALT_NUMBER)
            {
                // EOT unique predicts an alt
                dfaST.SetAttribute("eotPredictsAlt", EOTPredicts);
            }
            else if (EOTTarget != null && EOTTarget.NumberOfTransitions > 0)
            {
                // EOT state has transitions so must split on predicates.
                // Generate predicate else-if clauses and then generate
                // NoViableAlt exception as else clause.
                // Note: these predicates emanate from the EOT target state
                // rather than the current DFAState s so the error message
                // might be slightly misleading if you are looking at the
                // state number.  Predicates emanating from EOT targets are
                // hoisted up to the state that has the EOT edge.
                for (int i = 0; i < EOTTarget.NumberOfTransitions; i++)
                {
                    Transition     predEdge = (Transition)EOTTarget.GetTransition(i);
                    StringTemplate edgeST   = templates.GetInstanceOf(dfaEdgeName);
                    edgeST.SetAttribute("labelExpr",
                                        _parentGenerator.GenSemanticPredicateExpr(templates, predEdge));
                    // the target must be an accept state
                    //System.Console.Out.WriteLine( "EOT edge" );
                    StringTemplate targetST =
                        WalkFixedDFAGeneratingStateMachine(templates,
                                                           dfa,
                                                           (DFAState)predEdge.Target,
                                                           k + 1);
                    edgeST.SetAttribute("targetState", targetST);
                    dfaST.SetAttribute("edges", edgeST);
                }
            }
            return(dfaST);
        }
Esempio n. 23
0
 protected internal override bool EvalSemanticContext(SemanticContext pred, ParserRuleContext parserCallStack, int alt)
 {
     bool result = base.EvalSemanticContext(pred, parserCallStack, alt);
     if (!(pred is SemanticContext.PrecedencePredicate))
     {
         bool fullContext = _llStopIndex >= 0;
         int stopIndex = fullContext ? _llStopIndex : _sllStopIndex;
         decisions[currentDecision].predicateEvals.Add(new PredicateEvalInfo(currentState, currentDecision, _input, _startIndex, stopIndex, pred, result, alt));
     }
     return result;
 }
Esempio n. 24
0
        public static SemanticContext TranslateContext(LexicographerFileName fileName)
        {
            SemanticContext context = SemanticContext.None;

            switch (fileName)
            {
            case LexicographerFileName.NounAct:
                context = SemanticContext.Act;
                break;

            case LexicographerFileName.NounAnimal:
                context = SemanticContext.Animal;
                break;

            case LexicographerFileName.NounArtifact:
                context = SemanticContext.Artifact;
                break;

            case LexicographerFileName.NounAttribute:
                context = SemanticContext.Attribute;
                break;

            case LexicographerFileName.NounBody:
                context = SemanticContext.Body;
                break;

            case LexicographerFileName.NounCognition:
                context = SemanticContext.Cognition;
                break;

            case LexicographerFileName.NounCommunication:
                context = SemanticContext.Communication;
                break;

            case LexicographerFileName.NounEvent:
                context = SemanticContext.Event;
                break;

            case LexicographerFileName.NounFeeling:
                context = SemanticContext.Feeling;
                break;

            case LexicographerFileName.NounFood:
                context = SemanticContext.Food;
                break;

            case LexicographerFileName.NounGroup:
                context = SemanticContext.Group;
                break;

            case LexicographerFileName.NounLocation:
                context = SemanticContext.Location;
                break;

            case LexicographerFileName.NounMotive:
                context = SemanticContext.Motive;
                break;

            case LexicographerFileName.NounObject:
                context = SemanticContext.Object;
                break;

            case LexicographerFileName.NounPerson:
                context = SemanticContext.Person;
                break;

            case LexicographerFileName.NounPhenomenon:
                context = SemanticContext.Phenomenon;
                break;

            case LexicographerFileName.NounPlant:
                context = SemanticContext.Plant;
                break;

            case LexicographerFileName.NounPossession:
                context = SemanticContext.Possession;
                break;

            case LexicographerFileName.NounProcess:
                context = SemanticContext.Process;
                break;

            case LexicographerFileName.NounQuantity:
                context = SemanticContext.Quantity;
                break;

            case LexicographerFileName.NounRelation:
                context = SemanticContext.Relation;
                break;

            case LexicographerFileName.NounShape:
                context = SemanticContext.Shape;
                break;

            case LexicographerFileName.NounState:
                context = SemanticContext.State;
                break;

            case LexicographerFileName.NounSubstance:
                context = SemanticContext.Substance;
                break;

            case LexicographerFileName.NounTime:
                context = SemanticContext.Time;
                break;

            case LexicographerFileName.VerbBody:
                context = SemanticContext.Body;
                break;

            case LexicographerFileName.VerbChange:
                context = SemanticContext.Change;
                break;

            case LexicographerFileName.VerbCognition:
                context = SemanticContext.Cognition;
                break;

            case LexicographerFileName.VerbCommunication:
                context = SemanticContext.Communication;
                break;

            case LexicographerFileName.VerbCompetition:
                context = SemanticContext.Competition;
                break;

            case LexicographerFileName.VerbConsumption:
                context = SemanticContext.Consumption;
                break;

            case LexicographerFileName.VerbContact:
                context = SemanticContext.Contact;
                break;

            case LexicographerFileName.VerbCreation:
                context = SemanticContext.Creation;
                break;

            case LexicographerFileName.VerbEmotion:
                context = SemanticContext.Emotion;
                break;

            case LexicographerFileName.VerbMotion:
                context = SemanticContext.Motion;
                break;

            case LexicographerFileName.VerbPerception:
                context = SemanticContext.Perception;
                break;

            case LexicographerFileName.VerbPossession:
                context = SemanticContext.Possession;
                break;

            case LexicographerFileName.VerbSocial:
                context = SemanticContext.Social;
                break;

            case LexicographerFileName.VerbStative:
                context = SemanticContext.Stative;
                break;

            case LexicographerFileName.VerbWeather:
                context = SemanticContext.Weather;
                break;
            }

            return(context);
        }
Esempio n. 25
0
        protected override bool EvalSemanticContext(SemanticContext pred, ParserRuleContext parserCallStack, int alt, bool fullCtx)
        {
            bool result = base.EvalSemanticContext(pred, parserCallStack, alt, fullCtx);
            if (!(pred is SemanticContext.PrecedencePredicate)) {
            bool fullContext = llStopIndex >= 0;
            int stopIndex = fullContext ? llStopIndex : sllStopIndex;
            decisions[currentDecision].predicateEvals.Add(
                new PredicateEvalInfo(null , currentDecision, input, startIndex, stopIndex, pred, result, alt/*, fullCtx*/)
            );
            }

            return result;
        }
Esempio n. 26
0
 internal override void SemanticValidation(SemanticContext semanticContext)
 {
     throw new NotImplementedException();
 }
Esempio n. 27
0
 public virtual string GetPredicateDisplayString(SemanticContext.Predicate pred)
 {
     if (indexToPredMap == null)
     {
         indexToPredMap = GetIndexToPredicateMap();
     }
     ActionAST actionAST = indexToPredMap[pred.predIndex];
     return actionAST.Text;
 }
Esempio n. 28
0
        protected SemanticContext[] GetPredsForAmbigAlts(BitSet ambigAlts,
														 ATNConfigSet configSet,
													  int nalts)
        {
            // REACH=[1|1|[]|0:0, 1|2|[]|0:1]
            /* altToPred starts as an array of all null contexts. The entry at index i
             * corresponds to alternative i. altToPred[i] may have one of three values:
             *   1. null: no ATNConfig c is found such that c.alt==i
             *   2. SemanticContext.NONE: At least one ATNConfig c exists such that
             *      c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
             *      alt i has at least one unpredicated config.
             *   3. Non-NONE Semantic Context: There exists at least one, and for all
             *      ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
             *
             * From this, it is clear that NONE||anything==NONE.
             */
            SemanticContext[] altToPred = new SemanticContext[nalts + 1];
            foreach (ATNConfig c in configSet.configs)
            {
                if (ambigAlts[c.alt])
                {
                    altToPred[c.alt] = SemanticContext.OrOp(altToPred[c.alt], c.semanticContext);
                }
            }

            int nPredAlts = 0;
            for (int i = 1; i <= nalts; i++)
            {
                if (altToPred[i] == null)
                {
                    altToPred[i] = SemanticContext.NONE;
                }
                else if (altToPred[i] != SemanticContext.NONE)
                {
                    nPredAlts++;
                }
            }

            //		// Optimize away p||p and p&&p TODO: optimize() was a no-op
            //		for (int i = 0; i < altToPred.length; i++) {
            //			altToPred[i] = altToPred[i].optimize();
            //		}

            // nonambig alts are null in altToPred
            if (nPredAlts == 0) altToPred = null;
            if (debug) Console.WriteLine("getPredsForAmbigAlts result " + Arrays.ToString(altToPred));
            return altToPred;
        }
Esempio n. 29
0
 /**
  * Evaluate a semantic context within a specific parser context.
  *
  * <p>
  * This method might not be called for every semantic context evaluated
  * during the prediction process. In particular, we currently do not
  * evaluate the following but it may change in the future:</p>
  *
  * <ul>
  * <li>Precedence predicates (represented by
  * {@link SemanticContext.PrecedencePredicate}) are not currently evaluated
  * through this method.</li>
  * <li>Operator predicates (represented by {@link SemanticContext.AND} and
  * {@link SemanticContext.OR}) are evaluated as a single semantic
  * context, rather than evaluating the operands individually.
  * Implementations which require evaluation results from individual
  * predicates should override this method to explicitly handle evaluation of
  * the operands within operator predicates.</li>
  * </ul>
  *
  * @param pred The semantic context to evaluate
  * @param parserCallStack The parser context in which to evaluate the
  * semantic context
  * @param alt The alternative which is guarded by {@code pred}
  * @param fullCtx {@code true} if the evaluation is occurring during LL
  * prediction; otherwise, {@code false} if the evaluation is occurring
  * during SLL prediction
  *
  * @since 4.3
  */
 protected virtual bool EvalSemanticContext(SemanticContext pred, ParserRuleContext parserCallStack, int alt, bool fullCtx)
 {
     return pred.Eval(parser, parserCallStack);
 }
Esempio n. 30
0
 /** Given an arbitrarily complex SemanticContext, walk the "tree" and get display string.
  *  Pull predicates from grammar text.
  */
 public virtual string GetSemanticContextDisplayString(SemanticContext semctx)
 {
     if (semctx is SemanticContext.Predicate)
     {
         return GetPredicateDisplayString((SemanticContext.Predicate)semctx);
     }
     if (semctx is SemanticContext.AND)
     {
         SemanticContext.AND and = (SemanticContext.AND)semctx;
         return JoinPredicateOperands(and, " and ");
     }
     if (semctx is SemanticContext.OR)
     {
         SemanticContext.OR or = (SemanticContext.OR)semctx;
         return JoinPredicateOperands(or, " or ");
     }
     return semctx.ToString();
 }
Esempio n. 31
0
        public virtual string JoinPredicateOperands(SemanticContext.Operator op, string separator)
        {
            StringBuilder buf = new StringBuilder();
            foreach (SemanticContext operand in op.Operands)
            {
                if (buf.Length > 0)
                {
                    buf.Append(separator);
                }

                buf.Append(GetSemanticContextDisplayString(operand));
            }

            return buf.ToString();
        }
Esempio n. 32
0
        protected Symbol CreateTypes(ParseForest forest, SemanticContext context)
        {
            try
            {
                // "A wheel is a number" can define either a struct or a subclass.  Here we disambiguate.
                if (forest.SentenceType == 0 && forest.Count == 2 && forest.SentenceTypes().Aggregate("", (a, b) => a + " " + b) == " aSVO DeObj")
                {
                    forest.SentenceType = Rs.ObjectDefinition;
                }

                // Imperative functions span multiple sentences. Here, if the new sentence isn't an imperative, end any previous function body.
                if (AnInvokedImperative.Contains(forest.SentenceType))
                {
                    EndPreviousFunctionBody(context);
                    string.Join(" ", forest.a).Log();
                }

                // ok, start parsing based on sentence type
                switch (forest.SentenceType)
                {
                case Rs.FunctionDefinition:
                    DefineFuncSymbol(forest, context);
                    break;

                case Rs.ImperativeInvocationVO:
                case Rs.ImperativeInvocationSV:
                case Rs.ImperativeInvocationSVO:
                case Rs.ImperativeInvocationV:
                    DefineImpInvSymbol(forest, context);
                    break;

                case Rs.ObjectDefinition:
                    CreateSymbol(forest, new ParseTreeVisitor(forest), context);
                    break;

                case Rs.UnaryRelationDefinition:
                case Rs.RelationDefinition:
                    DefineRelationVerb(forest, context);
                    break;

                case Rs.TemporalConstraint:
                    DefineTemporalConstraint(forest, context);
                    break;

                case Rs.VarInstanceDefinition:
                    CreateSymbol(forest, new ParseTreeVisitor(forest), context);
                    break;

                case 0:
                    if (forest.Count == 0)
                    {
                        "ERROR. This doesn't parse:".Log(string.Join(" ", forest.a));
                        SyntaxParser.PrintPyramid(forest);
                    }
                    else
                    {
                        "ERROR. This is ambiguous:".Log(forest.SentenceTypes().Aggregate("", (a, b) => a + " " + b));
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                "EXCEPTION:".Log(ex.Message);
            }
            return(context.FunctionBeingDefined);
        }
Esempio n. 33
0
        /// <summary> Used by invocations. </summary>
        protected Symbol FillParameterWith_NP(ParseForest forest, ParseTreeVisitor visitor, SemanticContext context)
        {
            if (visitor.currentType != NonTerminalSymbol.The && visitor.currentType != NonTerminalSymbol.Adj && visitor.currentType != NonTerminalSymbol.N &&
                visitor.currentType != NonTerminalSymbol.AdN && visitor.currentType != NonTerminalSymbol.G && visitor.currentType != NonTerminalSymbol.NP)
            {
                throw new Exception(visitor.currentType + " was passed to FillParameterWith_NP; was expecting Adj, N, AdN, a, G");
            }
            string sym = "", det = "the";

            visitor.All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.The:
                    det = forest.a[i];
                    break;

                case NonTerminalSymbol.N:
                case NonTerminalSymbol.NP:
                case NonTerminalSymbol.Adj:
                    sym += (sym == "") ? forest.a[i] : " " + forest.a[i];
                    break;

                case NonTerminalSymbol.Rp:
                    PrintAST(forest);
                    throw new Exception("Visitor found a " + nt + " instead of a N, Adj, the/each, or NP.");

                default:
                    throw new Exception("Visitor found a " + nt + " instead of a N, Adj, the/each, or NP.");
                }
            });
            if (sym == "")
            {
                throw new Exception("sym is blank??? FillParameterWith_NP()");
            }

            det = (det == "several" || det == "many" || det == "each" || det == "every") ? "each " : "the ";
            Symbol symbol = Scope.Current.ResolveLoneSymbol(det + sym) ?? t.AsNumber(sym);

            if (symbol == null)
            {
                throw new UndefinedException(det + sym);
            }
            context.InvocationBeingDefined.SuppliedArguments.Add(new Parameter(context.prepFound, symbol));
            return(symbol);
        }
Esempio n. 34
0
        protected PredPrediction[] GetPredicatePredictions(BitSet ambigAlts,
																 SemanticContext[] altToPred)
        {
            List<PredPrediction> pairs = new List<PredPrediction>();
            bool containsPredicate = false;
            for (int i = 1; i < altToPred.Length; i++)
            {
                SemanticContext pred = altToPred[i];

                // unpredicated is indicated by SemanticContext.NONE

                if (ambigAlts != null && ambigAlts[i])
                {
                    pairs.Add(new PredPrediction(pred, i));
                }
                if (pred != SemanticContext.NONE) containsPredicate = true;
            }

            if (!containsPredicate)
            {
                return null;
            }

            //		Console.WriteLine(Arrays.toString(altToPred)+"->"+pairs);
            return pairs.ToArray();
        }
Esempio n. 35
0
 internal abstract void SemanticValidation(SemanticContext semanticContext);
Esempio n. 36
0
 public PredPrediction(SemanticContext pred, int alt)
 {
     // never null; at least SemanticContext.NONE
     this.alt  = alt;
     this.pred = pred;
 }
Esempio n. 37
0
 public ActionSemanticContextATNConfig(int actionIndex, SemanticContext semanticContext
     , ATNConfig c, ATNState state, PredictionContext context) : base(semanticContext
     , c, state, context)
 {
     this.actionIndex = actionIndex;
 }
Esempio n. 38
0
 public PredPrediction(SemanticContext pred, int alt)
 {
     // if accept, exec action in what rule?
     // if accept, exec what action?
     // never null; at least SemanticContext.NONE
     this.alt = alt;
     this.pred = pred;
 }
Esempio n. 39
0
        protected Symbol MatchGerundSignature(string gerund, ParseForest forest, ParseTreeVisitor visitor, SemanticContext context, bool isSubject)
        {
            string root = gerund.FormOf(Inflections.plural);

            "  Found gerund for".Log(root);
            var context2 = new SemanticContext {
                InvocationBeingDefined = new Invocation(root), SignatureBeingDefined = new Signature(root),
            };

            CreateSymbol(forest, visitor.Right(), context2);
            context2.InvocationBeingDefined.SuppliedArguments = context2.SignatureBeingDefined.parms;
            Scope.Current.ResolveMultimethodSymbolByArguments(context2.InvocationBeingDefined);
            if (context2.InvocationBeingDefined.FunctionSymbol == null)
            {
                throw new Backtrack("Signature of parameter-function \"" + context2.SignatureBeingDefined + "\" doesn't match any existing functions.");
            }

            var newSymbol = new Symbol
            {
                Name         = context2.SignatureBeingDefined.verb, // todo: might the name & type be backward? It's usually the other way but the logging looks better this way: it's more precise
                Type         = context2.SignatureBeingDefined.ToString(),
                Category     = Categories.Variable,                 // local variable
                PartOfSpeech = NonTerminalSymbol.GP,
            };

            context.SignatureBeingDefined[context.prepFound] = newSymbol;
            context.SignatureBeingDefined.HasSubject        |= isSubject;
            "  Higher-order function".Log("\"" + context.SignatureBeingDefined + "\" has a parameter \"" + context2.SignatureBeingDefined + "\" which could accept any \"" + context2.InvocationBeingDefined.FunctionSymbol.Signature + "\"");
            return(context2.InvocationBeingDefined.FunctionSymbol);
        }
Esempio n. 40
0
        /// <summary> Used by "A car is.." and "The corvette is..." but can possibly be used to create a symbol from any aNP or NP. </summary>
        protected Symbol CreateSymbolFromNP(ParseForest forest, ParseTreeVisitor visitor, SemanticContext context)
        {
            string adj = "", noun = "", det = "a";

            visitor.All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.The: det = forest.a[i]; break;

                case NonTerminalSymbol.a: det = forest.a[i]; break;

                case NonTerminalSymbol.Adj: adj += (adj == "") ? forest.a[i] : " " + forest.a[i]; break;

                case NonTerminalSymbol.N: noun += (noun == "") ? forest.a[i] : " " + forest.a[i]; break;

                default: throw new Exception("CreateSymbolFromSubject() visitor found a " + nt + " instead of a N or Adj.");
                }
            });

            if (adj == "" && noun == "")
            {
                throw new Exception("both adj & noun are blank???  CreateSymbolFromSubject()");
            }

            if (adj != "" && noun != "")
            {
                Symbol nountype = Scope.Current.ResolveLoneSymbol(noun);
                if (nountype != null)
                {
                    throw new RedeclareException(noun);
                }
                Symbol adjtype = (adj != "") ? Scope.Current.ResolveLoneSymbol(t.AdjToNounTypes.OrKey(adj)) : null;
                if (adjtype != null)
                {
                    throw new RedeclareException(adj);
                }
                throw new RedefineException(noun, adj);
            }

            string fullname  = (det != "a") ? "the " + (adj + " ").TrimStart() + noun : (noun != "") ? noun : adj;
            var    newSymbol = new Symbol
            {
                Name         = fullname,
                Type         = t.Something,
                Category     = (det != "a") ? /* Global */ Categories.Variable : Categories.ClassObjectStructType,
                PartOfSpeech = (fullname == adj) ? NonTerminalSymbol.Adj : NonTerminalSymbol.N,
            };

            "  DEFINED:".Log(newSymbol.Name);
            return(Scope.Current.Define(newSymbol));
        }
Esempio n. 41
0
 public ActionSemanticContextATNConfig(LexerActionExecutor lexerActionExecutor, SemanticContext semanticContext, ATNConfig c, ATNState state, PredictionContext context, bool passedThroughNonGreedyDecision)
     : base(semanticContext, c, state, context)
 {
     this.lexerActionExecutor = lexerActionExecutor;
     this.passedThroughNonGreedyDecision = passedThroughNonGreedyDecision;
 }
Esempio n. 42
0
 public PredPrediction(SemanticContext pred, int alt)
 {
     // never null; at least SemanticContext.NONE
     this.alt = alt;
     this.pred = pred;
 }
Esempio n. 43
0
        /// <summary> Used for parameters of a function, struct, etc. For subject, used by struct.</summary>
        protected Symbol CreateParameterFrom_aNP(ParseForest forest, ParseTreeVisitor visitor, SemanticContext context, bool isSubject = false)
        {
            if (visitor.currentType != NonTerminalSymbol.a && visitor.currentType != NonTerminalSymbol.Adj && visitor.currentType != NonTerminalSymbol.N &&
                visitor.currentType != NonTerminalSymbol.AdN && visitor.currentType != NonTerminalSymbol.aGP && visitor.currentType != NonTerminalSymbol.G && visitor.currentType != NonTerminalSymbol.aNP)
            {
                throw new Exception(visitor.currentType + " was passed to CreateParameterFrom_aNP; was expecting Adj, N, AdN, a, G, aGP");
            }
            string adj = "", noun = "", det = "a";

            visitor.All((nt, match, i, isLeaf) =>
            {
                if (!isLeaf)
                {
                    return;
                }
                switch (nt)
                {
                case NonTerminalSymbol.a: det = forest.a[i]; break;

                case NonTerminalSymbol.Adj: adj += (adj == "") ? forest.a[i] : " " + forest.a[i]; break;

                case NonTerminalSymbol.N: noun += (noun == "") ? forest.a[i] : " " + forest.a[i]; break;

                case NonTerminalSymbol.aNP: noun += (noun == "") ? forest.a[i] : " " + forest.a[i]; break;                         // "nothing"

                default:
                    Scope.Abandon();
                    throw new Exception("Visitor found a " + nt + " instead of a N, Adj, a/many, or aNP.");
                }
            });

            if (adj == "" && noun == "")
            {
                Scope.Abandon();
                throw new Exception("both adj & noun are blank??? CreateParameterFrom_aNP()");
            }

            Symbol nountype = Scope.Current.ResolveLoneSymbol(noun, Categories.ClassObjectStructType);
            Symbol adjtype  = (nountype == null && adj != "") ? Scope.Current.ResolveLoneSymbol(t.AdjToNounTypes.OrKey(adj), Categories.ClassObjectStructType) : null;

            if (nountype == null && adjtype == null)
            {
                Scope.Abandon();
                throw new Exception("neither '" + noun + "' nor '" + adj + "' are types in current scope. CreateParameterFrom_aNP()");
            }

            bool   dontFullyQualify = (adjtype == null && adj != "") || (adjtype != null && nountype == null) || (isSubject);
            string sym = (adjtype == null) ? (adj + " ").TrimStart() + noun : (nountype != null ? t.AdjToNounTypes.OrKey(adj) : noun);

            string fullName = dontFullyQualify ? sym : (sym + " " + (context.SignatureBeingDefined.verb == "" ? "" : "being " + context.SignatureBeingDefined.verb.FormOf(Inflections.past_participal)) + " " + context.prepFound).TrimEnd();

            det = (det == "several" || det == "many") ? "each " : "the ";

            string verb_s = context.SignatureBeingDefined.verb.FormOf(Inflections.plural);
            string verber = !isSubject ? "" : verb_s.EndsWith("e") ? det + verb_s + "r" : det + verb_s + "er";

            var newSymbol = new Symbol
            {
                Name         = det + fullName,
                Type         = (adjtype == null) ? nountype.Name : t.AdjToNounTypes.OrKey(adjtype.Name),
                Category     = Categories.Variable,             // local variable
                PartOfSpeech = NonTerminalSymbol.N,
                SecondName   = fullName.Contains(" being ") ? det + fullName.Substring(0, fullName.IndexOf(" being ")) : verber,
            };

            context.SignatureBeingDefined[context.prepFound] = newSymbol;
            context.SignatureBeingDefined.HasSubject        |= isSubject;
            "  PARAM:".Log(newSymbol.Name + "  (" + (det == "the " ? "a " : det) + newSymbol.Type + ") " + (newSymbol.SecondName == "" ? "" : "\"" + newSymbol.SecondName + "\""));
            return(Scope.Current.Define(newSymbol));
        }