Exemple #1
0
        static void Main(string[] args)
        {
            TextReader textReader = new StreamReader(@"test.json");
            var        sourceCode = textReader.ReadToEnd();

            var sourceCodeReader = new SourceCodeReader(sourceCode);

            var lexer = new Lexer(sourceCodeReader);


            Token token;

            Console.WriteLine("\t Generating Lexical Tokens...");
            do
            {
                token = lexer.GetNextToken();

                Console.WriteLine(string.Format("{0}", token));
            }while (token.TokenType != TokenType.EndOfFile);

            Console.WriteLine("\n\t Parsing Syntax...");

            ExecuteSyntaxTest(new Lexer(new SourceCodeReader(sourceCode)));

            Console.WriteLine("\n Syntax Analysis Complete. Validation Passed");

            Console.ReadKey();
        }
        public void GetReader_MoveIntoStreamCreateNewReaderAndCheckPosition()
        {
            // Setup tests data
            string testInput = _codeSnippets[CodeType.SevenLinesOfAssignemtStatements];

            // Count the code lines
            Regex           RE         = new Regex("\n", RegexOptions.Multiline);
            MatchCollection theMatches = RE.Matches(testInput);
            int             lines      = theMatches.Count + 1;

            // Create script source
            ScriptSource source = _testEng.CreateScriptSourceFromString(testInput,
                                                                        SourceCodeKind.Statements);
            // Create first reader
            SourceCodeReader srcFirstUnitReader = source.GetReader();

            // This could be a little fragile. Might be better just to hard code
            // expected value. - Save first line with first reader.
            Assert.IsTrue(srcFirstUnitReader.SeekLine(1));
            string expValue = srcFirstUnitReader.ReadLine();

            // Move to the middle of the stream (approximately).
            Assert.IsTrue(srcFirstUnitReader.SeekLine(lines / 2));

            // Create second unit reader
            SourceCodeReader srcSecondUnitReader = source.GetReader();

            Assert.AreEqual(srcSecondUnitReader.ReadLine(), expValue);
        }
        public void GivenASourceCodeString(string sourceCodeText)
        {
            var sourceCodeProvider = new SourceCodeReader(sourceCodeText);

            ScenarioContext.Current.Add("sourceCodeText", sourceCodeText);
            ScenarioContext.Current.Add("sourceCodeProvider", sourceCodeProvider);
        }
Exemple #4
0
        public void ReadToEndFromStringTextContentProvider()
        {
            string text = "abc";
            StringTextContentProvider provider = new StringTextContentProvider(text);

            using (SourceCodeReader reader = provider.GetReader()) {
                Assert.AreEqual("abc", reader.ReadToEnd());
            }
        }
        public SourceUnitTree Parse(SourceUnit /*!*/ sourceUnit, RubyCompilerOptions /*!*/ options, ErrorSink /*!*/ errorSink)
        {
            Assert.NotNull(sourceUnit, options, errorSink);

            ErrorCounter counter = new ErrorCounter(errorSink);

            _tokenizer.ErrorSink     = counter;
            _tokenizer.Compatibility = options.Compatibility;

            _lexicalScopes.Clear();

            EnterScope(CreateTopScope(options.LocalNames));

            using (SourceCodeReader reader = sourceUnit.GetReader()) {
                _sourceUnit = sourceUnit;
                _tokenizer.Initialize(null, reader, sourceUnit, options.InitialLocation);

                // Default encoding when hosted (ignore KCODE, we are reading from Unicode buffer):
                _tokenizer.Encoding = (reader.Encoding != null) ? RubyEncoding.GetRubyEncoding(reader.Encoding) : RubyEncoding.UTF8;
                _tokenizer.AllowNonAsciiIdentifiers = _tokenizer.Encoding != RubyEncoding.Binary;

                try {
                    Parse();
                    LeaveScope();
                } catch (InternalSyntaxError) {
                    _ast = null;
                    _lexicalScopes.Clear();
                } finally {
                    ScriptCodeParseResult props;
                    if (counter.AnyError)
                    {
                        _ast = null;

                        if (_tokenizer.UnterminatedToken)
                        {
                            props = ScriptCodeParseResult.IncompleteToken;
                        }
                        else if (_tokenizer.EndOfFileReached)
                        {
                            props = ScriptCodeParseResult.IncompleteStatement;
                        }
                        else
                        {
                            props = ScriptCodeParseResult.Invalid;
                        }
                    }
                    else
                    {
                        props = ScriptCodeParseResult.Complete;
                    }

                    sourceUnit.CodeProperties = props;
                }

                return(_ast);
            }
        }
Exemple #6
0
        public static void Execute(string inSRC, bool verbose, Dictionary <string, string> argsVariables)
        {
            string contenu = FileReader.ReadComments(inSRC, true);

            Console.WriteLine("\nSOURCE CODE");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(contenu);

            // COMPILE.
            SourceCodeReader reader  = new SourceCodeReader();
            Node             program = reader.Read(contenu);

            Console.WriteLine("\nTREE VIEW");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(program.Show());

            Console.WriteLine("\nCOMPILED SCRIPT (debug)");
            Console.WriteLine("-----------------------------------------------");
            string contenuBinDebug = CompilerWriter.Compile(reader.Read(contenu), true);

            Console.WriteLine(contenuBinDebug);

            string contenuBin = CompilerWriter.Compile(program, false);

            Console.WriteLine("\nCOMPILED SCRIPT (release)");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(contenuBin);
            File.WriteAllText("bin.txt", contenuBin);

            // PREPARE RUN.
            string contenuBinRead = FileReader.ReadComments(File.ReadAllText("bin.txt"), true);

            try
            {
                BasicRunner readerBin = new BasicRunner();

                // Init the interpreter.
                readerBin.Init(contenuBinRead);

                foreach (KeyValuePair <string, string> pair in argsVariables.ToArray())
                {
                    readerBin.Variables.Add(pair.Key, pair.Value);
                }

                // EXECUTE
                Console.WriteLine("\nSCRIPT EXECUTION");
                Console.WriteLine("-----------------------------------------------");
                readerBin.Execute(false, verbose);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine("Exception : " + e.Message);
                Console.ReadLine();
            }
        }
Exemple #7
0
        public SourceUnitTree Parse(SourceUnit /*!*/ sourceUnit, RubyCompilerOptions /*!*/ options, ErrorSink /*!*/ errorSink)
        {
            Assert.NotNull(sourceUnit, options, errorSink);

            ErrorCounter counter = new ErrorCounter(errorSink);

            _tokenizer.ErrorSink     = counter;
            _tokenizer.Compatibility = options.Compatibility;

            _lexicalScopes.Clear();

            EnterScope(CreateTopScope(options.LocalNames));

            using (SourceCodeReader reader = sourceUnit.GetReader()) {
                _sourceUnit = sourceUnit;
                _tokenizer.Initialize(null, reader, sourceUnit, options.InitialLocation);

                // default encoding when hosted:
                _encoding = reader.Encoding ?? RubyEncoding.GetDefaultHostEncoding(options.Compatibility);

                try {
                    Parse();
                    LeaveScope();
                } catch (InternalSyntaxError) {
                    _ast = null;
                    _lexicalScopes.Clear();
                } finally {
                    ScriptCodeParseResult props;
                    if (counter.AnyError)
                    {
                        _ast = null;

                        if (_tokenizer.UnterminatedToken)
                        {
                            props = ScriptCodeParseResult.IncompleteToken;
                        }
                        else if (_tokenizer.IsEndOfFile)
                        {
                            props = ScriptCodeParseResult.IncompleteStatement;
                        }
                        else
                        {
                            props = ScriptCodeParseResult.Invalid;
                        }
                    }
                    else
                    {
                        props = ScriptCodeParseResult.Complete;
                    }

                    sourceUnit.CodeProperties = props;
                }

                return(_ast);
            }
        }
Exemple #8
0
        public Lexer(CompilerContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Context    = context;
            sourceCode = context.SourceUnit.GetReader();
            reader     = new TokenizerBuffer(sourceCode, new SourceLocation(0, 1, 1), 4, true);
        }
        public override void Add(SourceUnit sourceUnit, string message, SourceSpan span, int errorCode, Severity severity)
        {
            if (severity == Severity.Warning && !ReportWarning(_context.Verbose, errorCode))
            {
                return;
            }

            CountError(severity);

            string       path;
            string       codeLine;
            RubyEncoding encoding;
            int          line = span.Start.Line;

            if (sourceUnit != null)
            {
                path = sourceUnit.Path;
                using (SourceCodeReader reader = sourceUnit.GetReader()) {
                    if (line > 0)
                    {
                        try {
                            reader.SeekLine(line);
                            codeLine = reader.ReadLine();
                        } catch (Exception) {
                            codeLine = null;
                        }
                    }
                    else
                    {
                        codeLine = null;
                    }
                    encoding = reader.Encoding != null?RubyEncoding.GetRubyEncoding(reader.Encoding) : RubyEncoding.UTF8;
                }
            }
            else
            {
                path     = null;
                codeLine = null;
                encoding = RubyEncoding.UTF8;
            }

            if (severity == Severity.Error || severity == Severity.FatalError)
            {
                throw new SyntaxError(message, path, line, span.Start.Column, codeLine);
            }
            else
            {
                WriteMessage(
                    MutableString.Create(RubyContext.FormatErrorMessage(message, "warning", path, line, span.Start.Column, null), encoding)
                    );
            }
        }
Exemple #10
0
        private static void TestcaseReader(string rawCode, params DepthText[] expectedTexts)
        {
            var reader = new SourceCodeReader(rawCode);

            foreach (var expText in expectedTexts)
            {
                reader.TryRead(out var actLine).IsTrue($"Expected line is {expText}, but Faield to read.");
                actLine.Text.Is(expText.Text, $"Expected read text is {expText.Text}, but actual text is {actLine.Text}");
                actLine.Depth.Is(expText.Depth, $"Expected depth of read text is {expText.Depth}, but actual text is {actLine.Depth}");
            }

            reader.TryRead(out var text).IsFalse($"Expected reader reached end of code, but read a text '{text}'");
        }
        public void GetReader_CreateMultipleDifferentInstances()
        {
            string       testInput = _codeSnippets[CodeType.SevenLinesOfAssignemtStatements];
            ScriptSource source    = _testEng.CreateScriptSourceFromString(testInput,
                                                                           SourceCodeKind.Statements);
            // Storage for readers to test
            SourceCodeReader prevStream = null, tmpStream = null;

            for (int i = 0; i < 10; i++)
            {
                if (i > 0)
                {
                    prevStream = tmpStream;
                }
                tmpStream = source.GetReader();
                Assert.AreNotEqual(prevStream, tmpStream);
            }
        }
Exemple #12
0
        /// <summary>
        /// If the SCRIPT_LINES__ constant is set, we need to publish the file being loaded,
        /// along with the contents of the file
        /// </summary>
        private void AddScriptLines(SourceUnit file)
        {
            ConstantStorage storage;

            if (!_context.ObjectClass.TryResolveConstant(null, "SCRIPT_LINES__", out storage))
            {
                return;
            }

            IDictionary scriptLines = storage.Value as IDictionary;

            if (scriptLines == null)
            {
                return;
            }

            lock (scriptLines) {
                // Read in the contents of the file

                RubyArray        lines    = new RubyArray();
                SourceCodeReader reader   = file.GetReader();
                RubyEncoding     encoding = RubyEncoding.GetRubyEncoding(reader.Encoding);
                using (reader) {
                    reader.SeekLine(1);
                    while (true)
                    {
                        string lineStr = reader.ReadLine();
                        if (lineStr == null)
                        {
                            break;
                        }
                        MutableString line = MutableString.CreateMutable(lineStr.Length + 1, encoding);
                        line.Append(lineStr).Append('\n');
                        lines.Add(line);
                    }
                }

                // Publish the contents of the file, keyed by the file name
                MutableString path = MutableString.Create(file.Document.FileName, _context.GetPathEncoding());
                scriptLines[path] = lines;
            }
        }
Exemple #13
0
        public void ResolveDependencies(IList <CodeUnit> resolvedUnits = null)
        {
            if (resolvedUnits == null)
            {
                resolvedUnits = new List <CodeUnit>();
            }

            var reader = SourceCodeReader.GetReaderFor(CurrentDependency);

            CodeLines = reader.ReadLines();

            var dependencies = _parser.Parse(CodeLines);

            if (!IsNamespaceDependency && !IsLibrary)
            {
                dependencies.Add(_namespaceDependency);
            }

            foreach (var dependency in dependencies)
            {
                dependency.Path.MakePathAbsolute(CurrentDependency.Path.Directory);

                var resolved = resolvedUnits.FirstOrDefault(u => u.CurrentDependency.Path.Equals(dependency.Path));

                if (resolved != null)
                {
                    Dependencies.Add(resolved);
                }
                else
                {
                    var newUnit = new CodeUnit(dependency, _namespaceDependency, _parser, CurrentDependency.IsNamespaceDependency);

                    Dependencies.Add(newUnit);
                    resolvedUnits.Add(newUnit);

                    OnResolve(newUnit.CurrentDependency.Path.Path);

                    newUnit.ResolveDependencies(resolvedUnits);
                }
            }
        }
Exemple #14
0
        private void GetNamespaceDependency()
        {
            var rootDependencies = _options
                                   .DependencyParser.Parse
                                   (
                SourceCodeReader
                .GetReaderFor(_options.RootDependency)
                .ReadLines()
                                   );

            var nsDependency = rootDependencies
                               .FirstOrDefault(d => d.IsNamespaceDependency);

            if (nsDependency == null)
            {
                throw new ArgumentException("No valid namespace defined in root.");
            }

            _namespaceDependency = nsDependency;
            _namespaceDependency.Path.MakePathAbsolute(_options.RootDependency.Path.Directory);
        }
Exemple #15
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            using (SourceCodeReader reader = sourceUnit.GetReader())
            {
                switch (sourceUnit.Kind)
                {
                case SourceCodeKind.File:
                case SourceCodeKind.AutoDetect:
                // TODO: add a different kind here!
                case SourceCodeKind.Expression:
                case SourceCodeKind.InteractiveCode:
                case SourceCodeKind.SingleStatement:
                case SourceCodeKind.Statements:
                    return(new AplusScriptCode(this.aplus,
                                               reader.ReadToEnd(),
                                               sourceUnit));

                case SourceCodeKind.Unspecified:
                default:

                    throw new Exception("SourceKind fail..");
                }
            }
        }
        public void GetReader_TwoIndependentReadersAccessingSameData()
        {
            string        testInput = _codeSnippets[CodeType.SevenLinesOfAssignemtStatements];
            StringBuilder strBuffer = new StringBuilder();

            //Get the aprox midpoint length of codinput.
            int codeMidPoint = testInput.Length / 2;

            ScriptSource source = _testEng.CreateScriptSourceFromString(testInput,
                                                                        SourceCodeKind.Statements);
            // Create the Readers
            SourceCodeReader srcFirstUnitReader  = source.GetReader();
            SourceCodeReader srcSecondUnitReader = source.GetReader();

            int chrnbr = 0;
            int cnt    = 0;

            // Read first half of stream with first stream reader
            while (((chrnbr = srcFirstUnitReader.Read()) > 0) && (cnt < codeMidPoint))
            {
                strBuffer.Append((char)chrnbr);
                cnt++; // inc cnt
                // Increment Second Reader
                srcSecondUnitReader.Read();
            }

            // Now get the second half of the input stream with second reader
            while ((chrnbr = srcSecondUnitReader.Read()) > 0)
            {
                strBuffer.Append((char)chrnbr);
                cnt++;
            }

            Assert.AreEqual(cnt, testInput.Length);
            Assert.AreEqual(testInput, strBuffer.ToString());
        }
 public override bool TryParse(SourceCodeReader reader, out object obj)
 {
     // This method is not be implemented, because purpose of this class is testing static methods
     throw new NotImplementedException();
 }
Exemple #18
0
        public virtual string Eval(string nom, int nbParams, LinkedList <string> param)
        {
            nom = nom.ToLower();

            if (nom == "()" && nbParams != 0)
            {
                return(param.ElementAt(nbParams - 1));
            }

            if ((nom == "=" || nom == "equals") && nbParams == 2)
            {
                return((param.ElementAt(0) == (param.ElementAt(1))) + "");
            }

            if (nom == "eval")
            {
                SourceCodeReader reader      = new SourceCodeReader();
                Node             program     = reader.Read(param.ElementAt(0));
                string           contenuBin  = CompilerWriter.Compile(program, false);
                BasicRunner      basicReader = new BasicRunner();
                basicReader.Init(contenuBin);
                basicReader.Execute(false, false);
            }

            if (nom == "call")
            {
                nom = param.ElementAt(0);
                nbParams--;
                param.RemoveFirst();
                return(Eval(nom, nbParams, param));
            }

            if (nom == "not" && nbParams == 1)
            {
                string condition = param.ElementAt(0);

                condition = condition.ToLower();

                return("" + (condition == "false" || condition == "0"));
            }

            if (nom == "or")
            {
                bool   continuer = nbParams > 0;
                int    i         = 0;
                string result    = "false";
                string condition;
                while (continuer)
                {
                    condition = param.ElementAt(i);
                    condition = condition.ToLower();

                    if (condition != "false" && condition != "0")
                    {
                        result = "true";
                        break;
                    }
                    i++;
                    continuer = i < nbParams;
                }
                return(result);
            }

            if (nom == "and")
            {
                bool   continuer = nbParams > 0;
                int    i         = 0;
                string result    = continuer + "";
                string condition;
                while (continuer)
                {
                    condition = param.ElementAt(i);
                    condition = condition.ToLower();

                    if (condition == "false" || condition == "0")
                    {
                        result = "false";
                        break;
                    }
                    i++;
                    continuer = i < nbParams;
                }
                return(result);
            }

            if (nom.StartsWith("read"))
            {
                switch (nom.Substring(4))
                {
                case "key":
                    Console.ReadKey();
                    return(null);

                case "line":
                    return(Console.ReadLine());

                default:
                    break;
                }
            }
            else if (nom == "print")
            {
                foreach (string s in param)
                {
                    Console.Write(s);
                }
                Console.WriteLine();
            }
            else if (nom == "substring")
            {
                if (nbParams == 2)
                {
                    return(param.ElementAt(0).Substring(TryParse(param.ElementAt(1))));
                }
                else if (nbParams == 3)
                {
                    int start  = TryParse(param.ElementAt(1));
                    int length = TryParse(param.ElementAt(2));

                    try
                    {
                        string s = param.ElementAt(0).Substring(start, length);
                        return(s);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else if (nom == "length" && nbParams == 1)
            {
                return(param.ElementAt(0).Length + "");
            }
            else if (nom == "replace" && nbParams == 3)
            {
                string exp      = param.ElementAt(0);
                string oldValue = param.ElementAt(1);
                string newValue = param.ElementAt(2);
                exp = exp.Replace(oldValue, newValue);
                return(exp);
            }
            else if (nom == "concat")
            {
                StringBuilder builder = new StringBuilder();
                foreach (string s in param)
                {
                    builder.Append(s);
                }
                return(builder.ToString());
            }
            else if (Regex.IsMatch(nom, "^[\\+|\\-|\\*|\\/|\\%]$") && nbParams == 2)
            {
                try
                {
                    return(EvalMath(nom[0], TryParse(param.ElementAt(0)),
                                    TryParse(param.ElementAt(1))));
                }
                catch (Exception e)
                {
                    return("Math Error : " + e.Message);
                }
            }

            return(null);        // Void.
        }