Parse() public method

public Parse ( ) : Story
return Ink.Parsed.Story
コード例 #1
0
ファイル: Compiler.cs プロジェクト: raphaelra/October2020
        public Runtime.Story Compile()
        {
            _parser = new InkParser(_inputString, _options.sourceFilename, OnError, _options.fileHandler);

            _parsedStory = _parser.Parse();

            if (_pluginManager != null)
            {
                _pluginManager.PostParse(_parsedStory);
            }

            if (_parsedStory != null && _errors.Count == 0)
            {
                _parsedStory.countAllVisits = _options.countAllVisits;

                _runtimeStory = _parsedStory.ExportRuntime(OnError);

                if (_pluginManager != null)
                {
                    _pluginManager.PostExport(_parsedStory, _runtimeStory);
                }
            }
            else
            {
                _runtimeStory = null;
            }

            return(_runtimeStory);
        }
コード例 #2
0
        protected object IncludeStatement()
        {
            Whitespace();

            if (ParseString("INCLUDE") == null)
            {
                return(null);
            }

            Whitespace();

            var filename = (string)Expect(() => ParseUntilCharactersFromString("\n\r"), "filename for include statement");

            filename = filename.TrimEnd(' ', '\t');

            var fullFilename = filename;

            if (_rootDirectory != null)
            {
                fullFilename = System.IO.Path.Combine(_rootDirectory, filename);
            }

            Parsed.Story includedStory  = null;
            string       includedString = null;

            try {
                includedString = System.IO.File.ReadAllText(fullFilename);
            }
            catch {
                string message = "Failed to load: " + filename;
                if (_rootDirectory != null)
                {
                    message += "' (root directory is " + _rootDirectory + "). File not found perhaps?";
                }
                Error(message);
            }


            if (includedString != null)
            {
                InkParser parser = new InkParser(includedString, filename, _rootDirectory);
                includedStory = parser.Parse();

                if (includedStory == null)
                {
                    // This error should never happen: if the includedStory isn't
                    // returned, then it should've been due to some error that
                    // has already been reported, so this is a last resort.
                    if (!parser.hadError)
                    {
                        Error("Failed to parse included file '" + filename);
                    }
                }
            }

            // Return valid IncludedFile object even when story failed to parse and we have a null story:
            // we don't want to attempt to re-parse the include line as something else
            return(new IncludedFile(includedStory));
        }
コード例 #3
0
        public static Runtime.Story Compile(string text, string name, ErrorHandler errorHandler)
        {
            var parser = new InkParser(text, name, errorHandler);
            var result = parser.Parse();
            var story  = result.ExportRuntime(errorHandler);

            return(story);
        }
コード例 #4
0
ファイル: InkParser_Include.cs プロジェクト: zhalesi/ink
        protected object IncludeStatement()
        {
            Whitespace();

            if (ParseString("INCLUDE") == null)
            {
                return(null);
            }

            Whitespace();

            var filename = (string)Expect(() => ParseUntilCharactersFromString("\n\r"), "filename for include statement");

            filename = filename.TrimEnd(' ', '\t');

            // Working directory should already have been set up relative to the root ink file.
            var workingDirectory = Directory.GetCurrentDirectory();
            var fullFilename     = System.IO.Path.Combine(workingDirectory, filename);

            if (FilenameIsAlreadyOpen(fullFilename))
            {
                Error("Recursive INCLUDE detected: '" + fullFilename + "' is already open.");
                ParseUntilCharactersFromString("\r\n");
                return(new IncludedFile(null));
            }
            else
            {
                AddOpenFilename(fullFilename);
            }

            Parsed.Story includedStory  = null;
            string       includedString = null;

            try {
                includedString = File.ReadAllText(fullFilename);
            }
            catch {
                Error("Failed to load: '" + filename + "' (relative to directory: " + workingDirectory + ")");
            }


            if (includedString != null)
            {
                InkParser parser = new InkParser(includedString, filename, _externalErrorHandler, _rootParser);
                includedStory = parser.Parse();
            }

            RemoveOpenFilename(fullFilename);

            // Return valid IncludedFile object even if there were errors when parsing.
            // We don't want to attempt to re-parse the include line as something else,
            // and we want to include the bits that *are* valid, so we don't generate
            // more errors than necessary.
            return(new IncludedFile(includedStory));
        }
コード例 #5
0
ファイル: InkParser_Include.cs プロジェクト: inkle/ink
        protected object IncludeStatement()
        {
            Whitespace ();

            if (ParseString ("INCLUDE") == null)
                return null;

            Whitespace ();

            var filename = (string) Expect(() => ParseUntilCharactersFromString ("\n\r"), "filename for include statement");
            filename = filename.TrimEnd (' ', '\t');

            // Working directory should already have been set up relative to the root ink file.
            var workingDirectory = Directory.GetCurrentDirectory ();
            var fullFilename = System.IO.Path.Combine (workingDirectory, filename);

            if (FilenameIsAlreadyOpen (fullFilename)) {
                Error ("Recursive INCLUDE detected: '" + fullFilename + "' is already open.");
                ParseUntilCharactersFromString("\r\n");
                return new IncludedFile(null);
            } else {
                AddOpenFilename (fullFilename);
            }

            Parsed.Story includedStory = null;
            string includedString = null;
            try {
                includedString = File.ReadAllText(fullFilename);
            }
            catch {
                Error ("Failed to load: '"+filename+"' (relative to directory: "+workingDirectory+")");
            }


            if (includedString != null ) {
                InkParser parser = new InkParser(includedString, filename, _externalErrorHandler, _rootParser);
                includedStory = parser.Parse();

                if( includedStory == null ) {
                    // This error should never happen: if the includedStory isn't
                    // returned, then it should've been due to some error that
                    // has already been reported, so this is a last resort.
                    if( !parser.hadError ) {
                        Error ("Failed to parse included file '" + filename);
                    }
                }
            }

            RemoveOpenFilename (fullFilename);

            // Return valid IncludedFile object even when story failed to parse and we have a null story:
            // we don't want to attempt to re-parse the include line as something else
            return new IncludedFile (includedStory);
        }
コード例 #6
0
ファイル: Tests.cs プロジェクト: y-lohse/ink
        public void TestEmptyChoice()
        {
            int warningCount = 0;
            InkParser parser = new InkParser("*", null, (string message, ErrorType errorType) =>
            {
                if (errorType == ErrorType.Warning)
                {
                    warningCount++;
                    Assert.IsTrue(message.Contains("completely empty"));
                }
                else
                {
                    Assert.Fail("Shouldn't have had any errors");
                }
            });

            parser.Parse();

            Assert.AreEqual(1, warningCount);
        }
コード例 #7
0
ファイル: Tests.cs プロジェクト: y-lohse/ink
        protected Ink.Parsed.Story CompileStringWithoutRuntime(string str, bool testingErrors = false)
        {
            _testingErrors = testingErrors;
            _errorMessages.Clear();
            _warningMessages.Clear();

            InkParser parser = new InkParser(str, null, TestErrorHandler);
            var parsedStory = parser.Parse();
            Assert.IsFalse(parsedStory.hadError);

            parsedStory.ExportRuntime(TestErrorHandler);
            return parsedStory;
        }
コード例 #8
0
ファイル: Tests.cs プロジェクト: y-lohse/ink
        // Helper compile function
        protected Story CompileString(string str, bool countAllVisits = false, bool testingErrors = false)
        {
            _testingErrors = testingErrors;
            _errorMessages.Clear();
            _warningMessages.Clear();

            InkParser parser = new InkParser(str, null, TestErrorHandler);
            var parsedStory = parser.Parse();
            parsedStory.countAllVisits = countAllVisits;

            Story story = parsedStory.ExportRuntime(TestErrorHandler);
            Assert.AreNotEqual(null, story);

            // Convert to json and back again
            if (_mode == TestMode.JsonRoundTrip)
            {
                var jsonStr = story.ToJsonString();
                story = new Story(jsonStr);
            }

            return story;
        }
コード例 #9
0
ファイル: Tests.cs プロジェクト: y-lohse/ink
        public void TestReturnTextWarning()
        {
            InkParser parser = new InkParser("== test ==\n return something",
                null,
                (string message, ErrorType errorType) =>
                {
                    if (errorType == ErrorType.Warning)
                    {
                        throw new TestWarningException();
                    }
                });

            Assert.Throws<TestWarningException>(() => parser.Parse());
        }
コード例 #10
0
 public Parsed.Story Parse()
 {
     _parser      = new InkParser(_inputString, _options.sourceFilename, OnParseError, _options.fileHandler);
     _parsedStory = _parser.Parse();
     return(_parsedStory);
 }
コード例 #11
0
    static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.WriteLine("Expected the root ink filename as an argument");
            return;
        }

        var filename = args[0];

        // Change working directory to wherever the main ink file is
        string absRootFilename;

        if (!Path.IsPathRooted(filename))
        {
            absRootFilename = Path.Combine(Directory.GetCurrentDirectory(), filename);
        }
        else
        {
            absRootFilename = filename;
        }

        Directory.SetCurrentDirectory(Path.GetDirectoryName(absRootFilename));
        filename = Path.GetFileName(absRootFilename);

        // Load up the ink
        var inkText = File.ReadAllText(filename);

        // No error handler, will just have an exception if it doesn't compile
        var parser = new Ink.InkParser(inkText, filename, fileHandler: new InkFileHandler());

        var story = parser.Parse();

        if (!story)
        {
            Console.WriteLine("ERROR: No story was produced?");
        }

        // Find all the text content
        var allStrings = story.FindAll <Ink.Parsed.Text>();

        // Count all the words across all strings
        var totalWords = 0;

        foreach (var text in allStrings)
        {
            var wordsInThisStr = 0;
            var wasWhiteSpace  = true;
            foreach (var c in text.text)
            {
                if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
                {
                    wasWhiteSpace = true;
                }
                else if (wasWhiteSpace)
                {
                    wordsInThisStr++;
                    wasWhiteSpace = false;
                }
            }

            totalWords += wordsInThisStr;
        }

        Console.WriteLine("Total words: " + totalWords);
    }
コード例 #12
0
        protected object IncludeStatement()
        {
            Whitespace();

            if (ParseString("INCLUDE") == null)
            {
                return(null);
            }

            Whitespace();

            var filename = (string)Expect(() => ParseUntilCharactersFromString("\n\r"), "filename for include statement");

            filename = filename.TrimEnd(' ', '\t');

            // Working directory should already have been set up relative to the root ink file.
            var workingDirectory = Directory.GetCurrentDirectory();
            var fullFilename     = System.IO.Path.Combine(workingDirectory, filename);

            if (FilenameIsAlreadyOpen(fullFilename))
            {
                Error("Recursive INCLUDE detected: '" + fullFilename + "' is already open.");
                ParseUntilCharactersFromString("\r\n");
                return(new IncludedFile(null));
            }
            else
            {
                AddOpenFilename(fullFilename);
            }

            Parsed.Story includedStory  = null;
            string       includedString = null;

            try {
                includedString = File.ReadAllText(fullFilename);
            }
            catch {
                Error("Failed to load: '" + filename + "' (relative to directory: " + workingDirectory + ")");
            }


            if (includedString != null)
            {
                InkParser parser = new InkParser(includedString, filename, _externalErrorHandler, _rootParser);
                includedStory = parser.Parse();

                if (includedStory == null)
                {
                    // This error should never happen: if the includedStory isn't
                    // returned, then it should've been due to some error that
                    // has already been reported, so this is a last resort.
                    if (!parser.hadError)
                    {
                        Error("Failed to parse included file '" + filename);
                    }
                }
            }

            RemoveOpenFilename(fullFilename);

            // Return valid IncludedFile object even when story failed to parse and we have a null story:
            // we don't want to attempt to re-parse the include line as something else
            return(new IncludedFile(includedStory));
        }