Ejemplo n.º 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);
        }
Ejemplo n.º 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);
 }
Ejemplo n.º 3
0
 public void Setup()
 {
     _grammar = new TestGrammar();
       _language = new LanguageData(_grammar);
       _parser = new Parser(_language);
       _context = _parser.Context;
 }
Ejemplo n.º 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}};
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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();
 }
Ejemplo n.º 11
0
 public LanguageRuntime(LanguageData language)
 {
     Language = language;
     NoneValue = NoneClass.Value;
     BuiltIns = new BindingSourceTable(Language.Grammar.CaseSensitive);
     Init();
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 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(); 
    }
Ejemplo n.º 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
Ejemplo n.º 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;
        }
Ejemplo n.º 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);
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 20
0
        public MainWindow()
        {
            InitializeComponent();

            grammar = new MathGrammar();
            language = new LanguageData(grammar);
            parser = new Parser(language);
        }
Ejemplo n.º 21
0
 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 23
0
 public DqlQueryReader(LanguageData languageData)
 {
     if (languageData == null)
     {
         throw new ArgumentNullException("languageData");
     }
     this.language = languageData;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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();
 }
Ejemplo n.º 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\")");
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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()");
 }
Ejemplo n.º 31
0
 public GrammarData(LanguageData language)
 {
     Language = language;
     Grammar  = language.Grammar;
 }
Ejemplo n.º 32
0
 protected internal virtual void CreateTokenFilters(LanguageData language, TokenFilterList filters)
 {
 }
Ejemplo n.º 33
0
 protected internal virtual void OnLanguageDataConstructed(LanguageData language)
 {
 }
Ejemplo n.º 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());
        }
Ejemplo n.º 35
0
 public Parser(LanguageData language) : this(language, null)
 {
 }
Ejemplo n.º 36
0
 public virtual void CreateTokenFilters(LanguageData language, TokenFilterList filters)
 {
 }
Ejemplo n.º 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.
 }
Ejemplo n.º 38
0
 public ParserData(LanguageData language)
 {
     Language = language;
 }
Ejemplo n.º 39
0
 public virtual void OnLanguageDataConstructed(LanguageData language)
 {
 }
Ejemplo n.º 40
0
 public ScannerData(LanguageData language)
 {
     Language = language;
 }
Ejemplo n.º 41
0
        }//method

        public virtual LanguageRuntime CreateRuntime(LanguageData data)
        {
            return(new LanguageRuntime(data));
        }
Ejemplo n.º 42
0
 public RunSampleArgs(LanguageData language, string sample, ParseTree parsedSample)
 {
     Language     = language;
     Sample       = sample;
     ParsedSample = parsedSample;
 }
Ejemplo n.º 43
0
 internal ScannerDataBuilder(LanguageData language)
 {
     _language    = language;
     _grammar     = _language.Grammar;
     _grammarData = language.GrammarData;
 }