Пример #1
0
        protected static MtCompiler InternCreateScriptApp(Stream output, Stream debugStream)
        {
            var grammar = new MtGrammar();
            var lang = new LanguageData(grammar);
            var parser = new Parser(grammar);
            var runtime = grammar.CreateRuntime(lang) as MultiTasksRuntime;

            if (output != null)
            {
                runtime.OutputStream = output;
            }
            #if DEBUG && !SILVERLIGHT

            if (debugStream != null)
            {
                // Add as a listener to debug
                var listener = new TextWriterTraceListener(debugStream);
                Debug.Listeners.Add(listener);
            }

            #endif
            var app = new ScriptApp(runtime);

            // Add constants
            app.Globals.Add("TRUE", MtResult.True);
            app.Globals.Add("FALSE", MtResult.False);

            #if DEBUG && !SILVERLIGHT
            MultiTasksRuntime.DebugDisplayInfo();
            #endif

            return new MtCompiler(app);
        }
Пример #2
0
 //Default constructor, creates default evaluator 
 public ExpressionEvaluator(ExpressionEvaluatorGrammar grammar) {
   Grammar = grammar;
   Language = new LanguageData(Grammar);
   Parser = new Parser(Language);
   Runtime = Grammar.CreateRuntime(Language);
   App = new ScriptApp(Runtime);
 }
Пример #3
0
 public void Setup()
 {
     _grammar = new TestGrammar();
       _language = new LanguageData(_grammar);
       _parser = new Parser(_language);
       _context = _parser.Context;
 }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Parser"/> class.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="scanner">The scanner.</param>
        /// <param name="root">The root.</param>
        /// <exception cref="Exception">
        ///   </exception>
        public Parser(LanguageData language, Scanner scanner, NonTerminal root)
        {
            Language = language;
            Context = new ParsingContext(this);
            Scanner = scanner ?? language.CreateScanner();

            if (Scanner != null)
            {
                Scanner.Initialize(this);
            } 
            else
            {
                Language.Errors.Add(GrammarErrorLevel.Error, null, "Scanner is not initialized for this grammar");
            }
            CoreParser = new CoreParser(this);
            Root = root;
            if (Root == null)
            {
                Root = Language.Grammar.Root;
                InitialState = Language.ParserData.InitialState;
            }
            else
            {
                if (Root != Language.Grammar.Root && !Language.Grammar.SnippetRoots.Contains(Root))
                {
                    throw new Exception(string.Format(Resources.ErrRootNotRegistered, root.Name));
                }

                InitialState = Language.ParserData.InitialStates[Root];
            }
        }
        public void Setup()
        {
            var grammar = new XamlMarkupExtensionGrammar();
            var language = new LanguageData(grammar);

            _parser = new Parser(language) {Context = {TracingEnabled = true}};
        }
Пример #6
0
 static void Main(string[] args) {
   Console.Title = "Irony Console Sample";
   Console.WriteLine("Irony Console Sample.");
   Console.WriteLine("");
   Console.WriteLine("Select a grammar to load:");
   Console.WriteLine("  1. Expression Evaluator");
   Console.WriteLine("  2. mini-Python");
   Console.WriteLine("  Or press any other key to exit.");
   Console.WriteLine("");
   Console.Write("?");
   var choice = Console.ReadLine();
   Grammar grammar; 
   switch (choice) {
     case "1": 
       grammar = new SampleExpressionEvaluatorGrammar();
       break;
     case "2":
       grammar = new MiniPython.MiniPythonGrammar();
       break; 
     default:
       return;
   }
   Console.Clear();
   var language = new LanguageData(grammar);
   var runtime = new LanguageRuntime(language);
   var commandLine = new CommandLine(runtime);
   commandLine.Run();
 }
 public static string PrintTerminals(LanguageData language)
 {
     var termList = language.GrammarData.Terminals.ToList();
     termList.Sort((x, y) => string.Compare(x.Name, y.Name));
     var result = string.Join(Environment.NewLine, termList);
     return result;
 }
Пример #8
0
 public ScriptApp(LanguageRuntime runtime)
 {
     Runtime = runtime;
       Language = Runtime.Language;
       DataMap = new AppDataMap(Language.Grammar.CaseSensitive);
       Init();
 }
 public override void BuildAst(LanguageData language, ParseTree parseTree) {
   var opHandler = new OperatorHandler(language.Grammar.CaseSensitive);
   Util.Check(!parseTree.HasErrors(), "ParseTree has errors, cannot build AST.");
   var astContext = new InterpreterAstContext(language, opHandler);
   var astBuilder = new AstBuilder(astContext);
   astBuilder.BuildAst(parseTree);
 }
 public void SetUp()
 {
     var grammar = new CustomDslGrammar();
     var language = new LanguageData(grammar);
     _parser = new Parser(language);
     _visitor = new CSharpExpressionBuilder();
 }
 public LanguageRuntime(LanguageData language)
 {
     Language = language;
     NoneValue = NoneClass.Value;
     BuiltIns = new BindingSourceTable(Language.Grammar.CaseSensitive);
     Init();
 }
Пример #12
0
 public ScriptInterpreter(LanguageData language) {
   Language = language;
   Runtime = Language.Grammar.CreateRuntime(Language);
   Parser = new Parser(Language);
   EvaluationContext = new EvaluationContext(Runtime);
   Status = _internalStatus = InterpreterStatus.Ready;
 }
Пример #13
0
        public static ParseTreeNode Parse(string src)
        {
            if (IsValid(src, new KISGrammer()))
            {
                var x = GetRoot(src, new KISGrammer());

                // DispTree(x, 100);
                return x;
            }
            else
            {
                LanguageData language = new LanguageData(new KISGrammer());

                Parser parser = new Parser(language);

                ParseTree parseTree = parser.Parse(src);

                foreach (var i in parseTree.ParserMessages)
                {
                    ErrorStack.Add(i.Message + " on line: " + (i.Location.Line + 1) + " Column: " + (i.Location.Column + 1) + " at Position: " + (i.Location.Position + 1));
                }

                return null;
            }
        }
Пример #14
0
    public ParseTree LastScript { get; private set; } //the root node of the last executed script


    #region Constructors
    public ScriptApp(LanguageData language) {
      Language = language;
      var grammar = language.Grammar as InterpretedLanguageGrammar;
      Runtime = grammar.CreateRuntime(language);
      DataMap = new AppDataMap(Language.Grammar.CaseSensitive); 
      Init(); 
    }
Пример #15
0
 public override void Apply(LanguageData language, LRItem owner) {
   var state = owner.State;
   var conflicts = state.BuilderData.Conflicts;
   if (conflicts.Count == 0) return;
   switch (ActionType) {
     case PreferredActionType.Shift:
       var currTerm = owner.Core.Current as Terminal;
       if (currTerm == null || !conflicts.Contains(currTerm)) return; //nothing to do
       //Current term for shift item (hint owner) is a conflict - resolve it with shift action
       var newState = owner.ShiftedItem.State;
       var shiftAction = new ShiftParserAction(owner);
       state.Actions[currTerm] = shiftAction;
       conflicts.Remove(currTerm);
       return;
     case PreferredActionType.Reduce:
       if (!owner.Core.IsFinal) return; //we take care of reduce items only here
       //we have a reduce item with "Reduce" hint. Check if any of lookaheads are in conflict
       ReduceParserAction reduceAction = null;
       foreach (var lkhead in owner.Lookaheads)
         if (conflicts.Contains(lkhead)) {
           if (reduceAction == null)
             reduceAction = new ReduceParserAction(owner.Core.Production);
           state.Actions[lkhead] = reduceAction;
           conflicts.Remove(lkhead);
         }
       return;
   }//switch
 }//method
Пример #16
0
        public bool AssemblyBlock(eBLOCK200_BLOCKTYPES blockType, int blockNr, eBLOCK200_OB_VERSIONS blockVer, 
            bool forceExpandedFormat, bool bNoWizardInfo, bool bUseChineseOpcode,
            string source, out byte[] dest)
        {
            dest = null;

            var server = new CoMsSig200Lib.Sig200ServerClass();
            Sig200SignatureList InsList = null;
            server.CreateInstructionSignatures(null);
            server.SetMnemonic(eSIG200_MNEMONICS.eSIG200_MNEMONIC_SIMATIC);
            server.GetInstructionSignatureList(ref InsList);

            var grammar = new STLCompiler.STLGrammar();
            LanguageData language = new LanguageData(grammar);
            Parser parser = new Parser(language);
            ParseTree parseTree = parser.Parse(source);
            ParseTreeNode root = parseTree.Root;

            var project = new STLCompiler.S7Project(root.AstNode as Block, server);
            var builder = new CoMsBlock200Lib.Block200Class() as IBlock200Fix;
            int pnOBSize, pnOBSizeNoWiz, pnDBArea3Size;

            builder.CalcBlockSizes(project, server,
                (int)eBLOCK200_OB_VERSIONS.eBLOCK200_OB_VERSION_2ND_GEN_5,
                2, /* Must 2, from reverse */
                out pnOBSize,
                out pnOBSizeNoWiz,
                out pnDBArea3Size);

            IntPtr ppbyDestBlock = Marshal.AllocCoTaskMem(pnOBSize);
            IntPtr ppbyEdgeData = IntPtr.Zero;
            int nDestBytes = pnOBSize, nEdges = 0;

            try
            {
                builder.AssembleBlock(project,
                server,
                (int)eBLOCK200_BLOCKTYPES.eBLOCK200_BLOCKTYPE_OB,
                1,
                (int)eBLOCK200_OB_VERSIONS.eBLOCK200_OB_VERSION_2ND_GEN_5,
                0,
                1,
                1,
                out ppbyDestBlock,
                out nDestBytes,
                out ppbyEdgeData,
                out nEdges);

                dest = new byte[nDestBytes];
                Marshal.Copy(ppbyDestBlock, dest, 0, nDestBytes);
             }
            catch(COMException e)
            {
                Console.Write(e.Message);
                return false;
            }

            return true;
        }
Пример #17
0
 public virtual void BuildAst(LanguageData language, ParseTree parseTree)
 {
     if (!LanguageFlags.IsSet(LanguageFlags.CreateAst))
         return;
     var astContext = new AstContext(language);
     var astBuilder = new AstBuilder(astContext);
     astBuilder.BuildAst(parseTree);
 }
Пример #18
0
 public static ParseTree GenerateParseTree(string statement)
 {
     Grammar grammar = new ID3SQLGrammar();
     LanguageData languageData = new LanguageData(grammar);
     Parser parser = new Parser(languageData);
     ParseTree parseTree = parser.Parse(statement);
     return parseTree;
 }
Пример #19
0
 //Default constructor, creates default evaluator
 public ReportingExpressionEvaluator(InterpretedLanguageGrammar grammar)
 {
     Grammar = grammar;
       Language = new LanguageData(Grammar);
       Parser = new Parser(Language);
       Runtime = Grammar.CreateRuntime(Language);
       App = new ScriptApp(Runtime);
 }
Пример #20
0
        public MainWindow()
        {
            InitializeComponent();

            grammar = new MathGrammar();
            language = new LanguageData(grammar);
            parser = new Parser(language);
        }
 public ParseTreeNode getRoot(string sourceCode, Grammar fabricGrammar)
 {
     LanguageData fabric = new LanguageData(fabricGrammar);
     Parser parser = new Parser(fabric);
     ParseTree parseTree = parser.Parse(sourceCode);
     ParseTreeNode root = parseTree.Root;
     return root;
 }
Пример #22
0
 private void Init(Grammar grammar) {
   _grammar = grammar;
   _language = new LanguageData(_grammar); 
   var parser = new Parser(_language);
   _scanner = parser.Scanner;
   _context = parser.Context;
   _context.Mode = ParseMode.VsLineScan;
 }
Пример #23
0
 public DqlQueryReader(LanguageData languageData)
 {
     if (languageData == null)
     {
         throw new ArgumentNullException("languageData");
     }
     this.language = languageData;
 }
Пример #24
0
 public EditorAdapter(LanguageData language) {
   _parser = new Parser(language);
   _scanner = _parser.Scanner;
   _colorizerThread = new Thread(ColorizerLoop);
   _colorizerThread.IsBackground = true;
   _parserThread = new Thread(ParserLoop);
   _parserThread.IsBackground = true;
 }
Пример #25
0
        public SqlDefaultParser(SqlGrammarBase grammar)
        {
            languageData = new LanguageData(grammar);
            parser = new Irony.Parsing.Parser(languageData);

            if (!languageData.CanParse())
                throw new InvalidOperationException();
        }
 public static string PrintTerminals(LanguageData language) {
   StringBuilder sb = new StringBuilder();
   foreach (Terminal term in language.GrammarData.Terminals) {
     sb.Append(term.ToString());
     sb.AppendLine();
   }
   return sb.ToString();
 }
Пример #27
0
        public void xBimSelectFunctionsWithStrings()
        {
            var grammar = new xBimQueryLanguage();
            var language = new LanguageData(grammar);
            var parser = new Parser(language);

            MustRunOk(parser, "select @12275.Representation.Representations.Where(Identifier=='Body')");
            MustRunOk(parser, "select @12275.Representation.Representations.Where(Identifier==\"Body\")");
        }
Пример #28
0
 public override void Apply(LanguageData language, Construction.LRItem owner) {
   //Check that owner is not final - we can imply precedence only in shift context
   var curr = owner.Core.Current;
   if (curr == null) 
     return;
   //mark the state, to make sure we do stuff in Term_Shifting event handler only in appropriate states
   owner.State.CustomFlags |= ImpliedPrecedenceCustomFlag; 
   curr.Shifting += Term_Shifting;
 }
Пример #29
0
 private void ParseSourceCode()
 {
     LanguageData language = new LanguageData(grammar);
     Parser gridWorldParser = new Parser(language);
     ParseTree parseTree = gridWorldParser.Parse(immediateRepresentation.ZestSourceCode);
     immediateRepresentation.HasErrors = parseTree.HasErrors();
     immediateRepresentation.ParseTreeRoot = parseTree.Root;
     immediateRepresentation.ErrorList = parseTree.ParserMessages;
 }
Пример #30
0
 public void xBimMultipleSelectRoots()
 {
     var grammar = new xBimQueryLanguage();
     var language = new LanguageData(grammar);
     var parser = new Parser(language);
     MustRunOk(parser, "select @IfcWall,IfcWallStandardCase.Where()");
     MustRunOk(parser, "select @IfcWall,IfcWallStandardCase,3.Where()");
     MustRunOk(parser, "select @3,IfcWallStandardCase,7.Where()");
 }
Пример #31
0
 public GrammarData(LanguageData language)
 {
     Language = language;
     Grammar  = language.Grammar;
 }
Пример #32
0
 protected internal virtual void CreateTokenFilters(LanguageData language, TokenFilterList filters)
 {
 }
Пример #33
0
 protected internal virtual void OnLanguageDataConstructed(LanguageData language)
 {
 }
Пример #34
0
        public static string PrintStateList(LanguageData language)
        {
            StringBuilder sb = new StringBuilder();

            foreach (ParserState state in language.ParserData.States)
            {
                sb.Append("State " + state.Name);
                if (state.BuilderData.IsInadequate)
                {
                    sb.Append(" (Inadequate)");
                }
                sb.AppendLine();
                var srConflicts = state.BuilderData.GetShiftReduceConflicts();
                if (srConflicts.Count > 0)
                {
                    sb.AppendLine("  Shift-reduce conflicts on inputs: " + srConflicts.ToString());
                }
                var ssConflicts = state.BuilderData.GetReduceReduceConflicts();
                if (ssConflicts.Count > 0)
                {
                    sb.AppendLine("  Reduce-reduce conflicts on inputs: " + ssConflicts.ToString());
                }
                //LRItems
                if (state.BuilderData.ShiftItems.Count > 0)
                {
                    sb.AppendLine("  Shift items:");
                    foreach (var item in state.BuilderData.ShiftItems)
                    {
                        sb.AppendLine("    " + item.ToString());
                    }
                }
                if (state.BuilderData.ReduceItems.Count > 0)
                {
                    sb.AppendLine("  Reduce items:");
                    foreach (LRItem item in state.BuilderData.ReduceItems)
                    {
                        var sItem = item.ToString();
                        if (item.Lookaheads.Count > 0)
                        {
                            sItem += " [" + item.Lookaheads.ToString() + "]";
                        }
                        sb.AppendLine("    " + sItem);
                    }
                }
                sb.Append("  Transitions: ");
                bool atFirst = true;
                foreach (BnfTerm key in state.Actions.Keys)
                {
                    ParserAction action         = state.Actions[key];
                    var          hasTransitions = action.ActionType == ParserActionType.Shift || action.ActionType == ParserActionType.Operator;
                    if (!hasTransitions)
                    {
                        continue;
                    }
                    if (!atFirst)
                    {
                        sb.Append(", ");
                    }
                    atFirst = false;
                    sb.Append(key.ToString());
                    sb.Append("->");
                    sb.Append(action.NewState.Name);
                }
                sb.AppendLine();
                sb.AppendLine();
            }//foreach
            return(sb.ToString());
        }
Пример #35
0
 public Parser(LanguageData language) : this(language, null)
 {
 }
Пример #36
0
 public virtual void CreateTokenFilters(LanguageData language, TokenFilterList filters)
 {
 }
Пример #37
0
 /// <summary> Gives a chance to a custom code in hint to interfere in parser automaton construction.</summary>
 /// <param name="language">The LanguageData instance.</param>
 /// <param name="owner">The LRItem that "owns" the hint. </param>
 /// <remarks>
 /// The most common purpose of this method (it's overrides) is to resolve the conflicts
 /// by adding specific actions into State.Actions dictionary.
 /// The owner parameter represents the position in the grammar expression where the hint
 /// is found. The parser state is available through owner.State property.
 /// </remarks>
 public virtual void Apply(LanguageData language, LRItem owner)
 {
     // owner.State  -- the parser state
     // owner.State.BuilderData.Conflicts -- as set of conflict terminals
     // owner.State.Actions -- a dictionary of actions in the current state.
 }
Пример #38
0
 public ParserData(LanguageData language)
 {
     Language = language;
 }
Пример #39
0
 public virtual void OnLanguageDataConstructed(LanguageData language)
 {
 }
Пример #40
0
 public ScannerData(LanguageData language)
 {
     Language = language;
 }
Пример #41
0
        }//method

        public virtual LanguageRuntime CreateRuntime(LanguageData data)
        {
            return(new LanguageRuntime(data));
        }
Пример #42
0
 public RunSampleArgs(LanguageData language, string sample, ParseTree parsedSample)
 {
     Language     = language;
     Sample       = sample;
     ParsedSample = parsedSample;
 }
Пример #43
0
 internal ScannerDataBuilder(LanguageData language)
 {
     _language    = language;
     _grammar     = _language.Grammar;
     _grammarData = language.GrammarData;
 }