├── .gitignore ├── LICENSE.txt ├── README.md ├── packagespecs ├── VBF.Compilers.Common │ └── VBF.Compilers.Common.nuspec ├── VBF.Compilers.Parsers │ └── VBF.Compilers.Parsers.nuspec └── VBF.Compilers.Scanners │ └── VBF.Compilers.Scanners.nuspec ├── resign.cmd └── src ├── Compilers ├── .nuget │ ├── NuGet.Config │ ├── NuGet.targets │ └── nuget.exe ├── Compilers.Common │ ├── CodeContract.cs │ ├── Common │ │ ├── DepthFirstSearch.cs │ │ ├── DisjointSets.cs │ │ ├── EditDistanceCalculator.cs │ │ ├── PriorityQueue.cs │ │ └── StringHelpers.cs │ ├── CompilationError.cs │ ├── CompilationErrorInfo.cs │ ├── CompilationErrorInfoCollection.cs │ ├── CompilationErrorList.cs │ ├── CompilationErrorManager.cs │ ├── CompilationStage.cs │ ├── Compilers.Common.csproj │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── RevertPoint.cs │ ├── RevertPointCollection.cs │ ├── SourceLocation.cs │ ├── SourceReader.cs │ ├── SourceSpan.cs │ └── StringBuilderReader.cs ├── Compilers.Intermediate │ ├── AssignInstruction.cs │ ├── BinaryExpression.cs │ ├── Compilers.Intermediate.csproj │ ├── ConstValue.cs │ ├── ConvertExpression.cs │ ├── Expression.cs │ ├── GotoInstruction.cs │ ├── Instruction.cs │ ├── Label.cs │ ├── Operand.cs │ ├── PrimaryType.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ReceiveInstruction.cs │ ├── TreeLang.cs │ ├── UnaryExpression.cs │ └── Variable.cs ├── Compilers.Parsers.Combinators │ ├── AlternationParser.cs │ ├── AnyTokenParser.cs │ ├── Compilers.Parsers.Combinators.csproj │ ├── ConcatenationParser.cs │ ├── ConvertHelper.cs │ ├── EndOfStreamParser.cs │ ├── ErrorCorrection.cs │ ├── MappingParser.cs │ ├── Parser.cs │ ├── ParserContext.cs │ ├── ParserFrame.cs │ ├── ParserReference.cs │ ├── ParserRunner.cs │ ├── Parsers.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── RepeatParserListNode.cs │ ├── Result.cs │ ├── SucceedParser.cs │ └── TokenParser.cs ├── Compilers.Parsers │ ├── AlternationProduction.cs │ ├── AmbiguityAggregator.cs │ ├── Compilers.Parsers.csproj │ ├── ConcatenationProduction.cs │ ├── DefaultValueContainer.cs │ ├── EmptyProduction.cs │ ├── EndOfStream.cs │ ├── ErrorRecord.cs │ ├── Generator │ │ ├── ClosureVisitor.cs │ │ ├── GeneratorVisitors.cs │ │ ├── LR0Edge.cs │ │ ├── LR0Item.cs │ │ ├── LR0Model.cs │ │ ├── LR0State.cs │ │ ├── ProductionInfo.cs │ │ ├── ProductionInfoManager.cs │ │ ├── ReduceAction.cs │ │ ├── SymbolConversion.cs │ │ └── TransitionTable.cs │ ├── Grammar.cs │ ├── IProduction.cs │ ├── IProductionVisitor.cs │ ├── MappingProduction.cs │ ├── PanicRecoverException.cs │ ├── ParserBase.cs │ ├── ParserEngine.cs │ ├── ParserHead.cs │ ├── ParserHeadCleaner.cs │ ├── ParsingFailureException.cs │ ├── Production.cs │ ├── ProductionBase.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ReduceVisitor.cs │ ├── RepeatParserListNode.cs │ ├── ResultInfo.cs │ ├── SetHelpers.cs │ ├── StackNode.cs │ ├── SyntaxErrors.cs │ └── Terminal.cs ├── Compilers.Scanners │ ├── AlternationCharSetExpression.cs │ ├── AlternationExpression.cs │ ├── CacheQueue.cs │ ├── CharSetExpressionBuilder.cs │ ├── Compilers.Scanners.csproj │ ├── ConcatenationExpression.cs │ ├── EmptyExpression.cs │ ├── FiniteAutomationEngine.cs │ ├── ForkableScanner.cs │ ├── ForkableScannerBuilder.cs │ ├── ForkableScannerCore.cs │ ├── Generator │ │ ├── CompactCharSetManager.cs │ │ ├── CompressedTransitionTable.cs │ │ ├── DFAEdge.cs │ │ ├── DFAModel.cs │ │ ├── DFAState.cs │ │ ├── NFAConverter.cs │ │ ├── NFAEdge.cs │ │ ├── NFAModel.cs │ │ └── NFAState.cs │ ├── HistoryList.cs │ ├── KleeneStarExpression.cs │ ├── Lexeme.cs │ ├── LexemeRange.cs │ ├── LexemeValue.cs │ ├── Lexer.cs │ ├── Lexicon.cs │ ├── PeekableScanner.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── RegularExpression.cs │ ├── RegularExpressionConverter.cs │ ├── RegularExpressionType.cs │ ├── Scanner.cs │ ├── ScannerException.cs │ ├── ScannerInfo.cs │ ├── StringLiteralExpression.cs │ ├── SymbolExpression.cs │ ├── Token.cs │ ├── TokenInfo.cs │ └── UnitTestVisibility.cs ├── Compilers.UnitTests │ ├── CommonTests.cs │ ├── Compilers.UnitTests.csproj │ ├── Compilers.UnitTests.snk │ ├── GrammarTests.cs │ ├── ParserBuilderTest.cs │ ├── ParserCombinatorsTest.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ScannersTest.cs │ └── packages.config ├── Compilers.sln ├── Compilers.sln.DotSettings └── Key │ ├── PublicKeyAndToken.txt │ └── VBF.Public.snk ├── Samples └── MiniSharp │ ├── ArrayType.cs │ ├── Ast │ ├── ArrayAssign.cs │ ├── ArrayLength.cs │ ├── ArrayLookup.cs │ ├── Assign.cs │ ├── AstNode.cs │ ├── AstVisitor.cs │ ├── Binary.cs │ ├── Block.cs │ ├── BooleanLiteral.cs │ ├── BooleanType.cs │ ├── Call.cs │ ├── ClassDecl.cs │ ├── Expression.cs │ ├── FieldDecl.cs │ ├── Formal.cs │ ├── IAstVisitor.cs │ ├── IdentifierType.cs │ ├── IfElse.cs │ ├── IntArrayType.cs │ ├── IntegerLiteral.cs │ ├── IntegerType.cs │ ├── MainClass.cs │ ├── MethodDecl.cs │ ├── MethodRef.cs │ ├── NewArray.cs │ ├── NewObject.cs │ ├── Not.cs │ ├── Program.cs │ ├── Statement.cs │ ├── This.cs │ ├── Type.cs │ ├── TypeConvert.cs │ ├── TypeRef.cs │ ├── VarDecl.cs │ ├── Variable.cs │ ├── VariableRef.cs │ ├── While.cs │ └── WriteLine.cs │ ├── CodeClassType.cs │ ├── ExtensionTable.cs │ ├── Field.cs │ ├── MemberDeclResolver.cs │ ├── Method.cs │ ├── MethodBodyResolver.cs │ ├── MethodOverloadingComparer.cs │ ├── MiniSharp.csproj │ ├── MiniSharp.sln │ ├── MiniSharpParser.CP.cs │ ├── MiniSharpParser.cs │ ├── Parameter.cs │ ├── PrimaryType.cs │ ├── ProgramEntry.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Targets │ └── Cil │ │ └── EmitTranslator.cs │ ├── TypeBase.cs │ ├── TypeCollection.cs │ ├── TypeDeclResolver.cs │ ├── VariableCollection.cs │ ├── VariableInfo.cs │ └── app.config └── temp ├── CodeDomTranslator.cs ├── ForkNode.cs ├── Parser.cs ├── ParserFunc.cs └── Parsers.cs /.gitignore: -------------------------------------------------------------------------------- 1 | #Unit tests and NuGet packages 2 | TestResults 3 | packages 4 | 5 | ################# 6 | ## Visual Studio 7 | ################# 8 | 9 | ## Ignore Visual Studio temporary files, build results, and 10 | ## files generated by popular Visual Studio add-ons. 11 | 12 | # User-specific files 13 | *.suo 14 | *.user 15 | *.sln.docstates 16 | 17 | # Build results 18 | [Dd]ebug/ 19 | [Rr]elease/ 20 | *_i.c 21 | *_p.c 22 | *.ilk 23 | *.meta 24 | *.obj 25 | *.pch 26 | *.pdb 27 | *.pgc 28 | *.pgd 29 | *.rsp 30 | *.sbr 31 | *.tlb 32 | *.tli 33 | *.tlh 34 | *.tmp 35 | *.vspscc 36 | .builds 37 | *.dotCover 38 | 39 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 40 | #packages/ 41 | 42 | # Visual C++ cache files 43 | ipch/ 44 | *.aps 45 | *.ncb 46 | *.opensdf 47 | *.sdf 48 | 49 | # Visual Studio profiler 50 | *.psess 51 | *.vsp 52 | 53 | # ReSharper is a .NET coding add-in 54 | _ReSharper* 55 | 56 | # Installshield output folder 57 | [Ee]xpress 58 | 59 | # DocProject is a documentation generator add-in 60 | DocProject/buildhelp/ 61 | DocProject/Help/*.HxT 62 | DocProject/Help/*.HxC 63 | DocProject/Help/*.hhc 64 | DocProject/Help/*.hhk 65 | DocProject/Help/*.hhp 66 | DocProject/Help/Html2 67 | DocProject/Help/html 68 | 69 | # Click-Once directory 70 | publish 71 | 72 | # Others 73 | [Bb]in 74 | [Oo]bj 75 | sql 76 | TestResults 77 | *.Cache 78 | ClientBin 79 | stylecop.* 80 | ~$* 81 | *.dbmdl 82 | Generated_Code #added for RIA/Silverlight projects 83 | 84 | # Backup & report files from converting an old project file to a newer 85 | # Visual Studio version. Backup files are not needed, because we have git ;-) 86 | _UpgradeReport_Files/ 87 | Backup*/ 88 | UpgradeLog*.XML 89 | 90 | 91 | 92 | ############ 93 | ## Windows 94 | ############ 95 | 96 | # Windows image file caches 97 | Thumbs.db 98 | 99 | # Folder config file 100 | Desktop.ini 101 | 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | VBF 2 | ======== 3 | 4 | VBF is a set of tools/libraries for compilers. It has seperated libraries for scanners and parsers. One can easily use one component as well as use them all together. 5 | 6 | #### How to build: 7 | 1. Install Visual Studio 2015 with .Net Framework 4.5 8 | 2. Open Options dialog, go to "Package Manager" and then check "Allow NuGet to download missing packages during build" 9 | 3. Open Project VBF/src/Compilers/Compilers.sln 10 | 4. Build 11 | 5. Binaries will be placed in VBF/bin folder 12 | 6. Note that by default, all the binaries are delay signed. You can either disable code signing or use 'sn -Vr' command to bypass assembly validation and then test the binaries. 13 | 14 | #### Components: 15 | * VBF.Compilers.Common 16 | 17 | Provides source file reader, compilation error manager, etc 18 | 19 | * VBF.Compilers.Scanners 20 | 21 | Provides DFA based scanner builder using regular expressions. There're multiple types of scanners to choose: a standard Scanner, a PeekableScanner with the capability to peek n tokens, a ForkableScanner that can fork to multiple scanner heads. 22 | 23 | * VBF.Compilers.Parsers 24 | 25 | Provides powerful GLR based, auto error recovery parser generator. To start, inherit ParserBase class and build your own parser. It is recommended for most parsers. 26 | 27 | * VBF.Compilers.Parsers.Combinators 28 | 29 | Similar to Compilers.Parsers but it's an GLL based parser combinator library. The grammar supported by this library is relatively less powerfull compared to VBF.Compilers.Parsers, and it also parses slower. It is mainly used to study LL grammars. 30 | 31 | #### Samples: 32 | * MiniSharp: VBF\src\Samples\MiniSharp 33 | 34 | MiniSharp is a fully functional compiler of a very small subset of C# language. It contains parsers and scanners built with VBF and an MSIL code generator. 35 | 36 | It is a good sample for programming language and DSL authors. 37 | 38 | * For additional sample, please refer to NotBasic project: https://github.com/Ninputer/notbasic 39 | 40 | #### License 41 | 42 | All components and samples of this software are distributed under Apache License 2.0 43 | You can find a copy of license under the root directory 44 | 45 | #### Other information 46 | * Chinese blog: http://ninputer.cnblogs.com/ 47 | * Chinese Weibo: http://weibo.com/ninputer -------------------------------------------------------------------------------- /packagespecs/VBF.Compilers.Common/VBF.Compilers.Common.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | VBF.Compilers.Common 5 | 1.0.5 6 | VBF Compilers Common Components 7 | Fan Shi 8 | Fan Shi 9 | https://raw.githubusercontent.com/Ninputer/VBF/master/LICENSE.txt 10 | https://github.com/Ninputer/VBF 11 | true 12 | The common components for VBF.Compilers libraries 13 | 14 | Copyright (c) 2012 Fan Shi 15 | VBF Compiler 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /packagespecs/VBF.Compilers.Parsers/VBF.Compilers.Parsers.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | VBF.Compilers.Parsers 5 | 1.0.5 6 | VBF Compilers Library for Parsers 7 | Fan Shi 8 | Fan Shi 9 | https://raw.githubusercontent.com/Ninputer/VBF/master/LICENSE.txt 10 | https://github.com/Ninputer/VBF 11 | true 12 | VBF.Compilers.Parsers is a GLR parser generator library. Compose grammars using Linq clauses, parsers with automatic error recovery can then be generated at runtime. Any context free grammar is supported, grammars with ambiguities can still be used by providing resolution rules. 13 | 14 | Copyright (c) 2012 Fan Shi 15 | VBF Compiler parser yacc antlr LR GLR 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /packagespecs/VBF.Compilers.Scanners/VBF.Compilers.Scanners.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | VBF.Compilers.Scanners 5 | 1.0.5 6 | VBF Compilers Library for Scanners 7 | Fan Shi 8 | Fan Shi 9 | https://raw.githubusercontent.com/Ninputer/VBF/master/LICENSE.txt 10 | https://github.com/Ninputer/VBF 11 | true 12 | VBF.Compilers.Scanners is a scanner builder. It contains a regular expression to DFA engine, can generate high performance scanners for unicode source text. 13 | 14 | Copyright (c) 2012 Fan Shi 15 | VBF Compiler scanner lexer lex tokenizer 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /resign.cmd: -------------------------------------------------------------------------------- 1 | sn -R .\bin\VBF.Compilers.Common.dll .\bin\VBF.snk 2 | sn -R .\bin\VBF.Compilers.Scanners.dll .\bin\VBF.snk 3 | sn -R .\bin\VBF.Compilers.Parsers.dll .\bin\VBF.snk 4 | 5 | REM verify signing 6 | 7 | sn -vf .\bin\VBF.Compilers.Common.dll 8 | sn -vf .\bin\VBF.Compilers.Scanners.dll 9 | sn -vf .\bin\VBF.Compilers.Parsers.dll -------------------------------------------------------------------------------- /src/Compilers/.nuget/NuGet.Config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/Compilers/.nuget/nuget.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ninputer/VBF/bd504d6a62a15a351a0e8ff3ce9a13de3914da78/src/Compilers/.nuget/nuget.exe -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CodeContract.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers 20 | { 21 | static class CodeContract 22 | { 23 | public static void RequiresArgumentNotNull(object argValue, string argName) 24 | { 25 | if (argValue == null) 26 | { 27 | throw new ArgumentNullException(argName); 28 | } 29 | } 30 | 31 | public static void RequiresArgumentNotNull(object argValue, string argName, string message) 32 | { 33 | if (argValue == null) 34 | { 35 | throw new ArgumentNullException(argName, message); 36 | } 37 | } 38 | 39 | public static void RequiresArgumentInRange(bool isInRange, string argName, string message) 40 | { 41 | if (!isInRange) 42 | { 43 | throw new ArgumentOutOfRangeException(argName, message); 44 | } 45 | } 46 | 47 | public static void Requires(bool condition, string argName) 48 | { 49 | if (!condition) 50 | { 51 | throw new ArgumentException(argName); 52 | } 53 | } 54 | 55 | public static void Requires(bool condition, string argName, string message) 56 | { 57 | if (!condition) 58 | { 59 | throw new ArgumentException(argName, message); 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/Common/StringHelpers.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Common 18 | { 19 | public static class StringHelpers 20 | { 21 | public static int EditDistance(string str1, string str2) 22 | { 23 | var calc = new EditDistanceCalculator(str1, str2); 24 | 25 | return calc.Calculate(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CompilationError.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Diagnostics; 19 | 20 | namespace VBF.Compilers 21 | { 22 | [DebuggerDisplay("{ToString()}")] 23 | public class CompilationError 24 | { 25 | public CompilationError(CompilationErrorInfo errorInfo, SourceSpan errorPosition, string errorMessage) 26 | { 27 | Info = errorInfo; 28 | ErrorPosition = errorPosition; 29 | Message = errorMessage; 30 | } 31 | 32 | public CompilationErrorInfo Info { get; private set; } 33 | public string Message { get; private set; } 34 | public SourceSpan ErrorPosition { get; private set; } 35 | 36 | public override string ToString() 37 | { 38 | return String.Format("{0} : {1} Line: {2} Column: {3}", Info.Id, Message, ErrorPosition.StartLocation.Line, ErrorPosition.StartLocation.Column); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CompilationErrorInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers 18 | { 19 | public class CompilationErrorInfo 20 | { 21 | public CompilationErrorInfo(int id, int level, CompilationStage stage, string messageTemplate) 22 | { 23 | Id = id; 24 | Level = level; 25 | Stage = stage; 26 | MessageTemplate = messageTemplate; 27 | } 28 | 29 | public int Id { get; private set; } 30 | public int Level { get; private set; } 31 | public CompilationStage Stage { get; private set; } 32 | public string MessageTemplate { get; set; } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CompilationErrorInfoCollection.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.ObjectModel; 18 | 19 | namespace VBF.Compilers 20 | { 21 | public class CompilationErrorInfoCollection : KeyedCollection 22 | { 23 | 24 | protected override int GetKeyForItem(CompilationErrorInfo item) 25 | { 26 | return item.Id; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CompilationErrorList.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections; 19 | using System.Collections.Generic; 20 | 21 | namespace VBF.Compilers 22 | { 23 | public class CompilationErrorList : IReadOnlyList 24 | { 25 | private CompilationErrorManager m_errorManager; 26 | private List m_errors; 27 | 28 | public CompilationErrorList(CompilationErrorManager errorManager) 29 | { 30 | CodeContract.RequiresArgumentNotNull(errorManager, "errorManager"); 31 | m_errors = new List(); 32 | m_errorManager = errorManager; 33 | } 34 | 35 | public CompilationError this[int index] 36 | { 37 | get { return m_errors[index]; } 38 | } 39 | 40 | public int Count 41 | { 42 | get { return m_errors.Count; } 43 | } 44 | 45 | public IEnumerator GetEnumerator() 46 | { 47 | foreach (var item in m_errors) 48 | { 49 | yield return item; 50 | } 51 | } 52 | 53 | IEnumerator IEnumerable.GetEnumerator() 54 | { 55 | return GetEnumerator(); 56 | } 57 | 58 | public void AddError(int id, SourceSpan errorPosition, params object[] args) 59 | { 60 | CodeContract.RequiresArgumentInRange(m_errorManager.ContainsErrorDefinition(id), "id", "Error id is invalid"); 61 | 62 | var errorInfo = m_errorManager.GetErrorInfo(id); 63 | var errorMessage = String.Format(errorInfo.MessageTemplate, args); 64 | 65 | m_errors.Add(new CompilationError(errorInfo, errorPosition, errorMessage)); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CompilationErrorManager.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers 18 | { 19 | public class CompilationErrorManager 20 | { 21 | private CompilationErrorInfoCollection m_errorInfoStore; 22 | 23 | public CompilationErrorManager() 24 | { 25 | m_errorInfoStore = new CompilationErrorInfoCollection(); 26 | } 27 | 28 | public void DefineError(int id, int level, CompilationStage stage, string messageTemplate) 29 | { 30 | CodeContract.RequiresArgumentInRange(!m_errorInfoStore.Contains(id), "id", "Error id is duplicated"); 31 | 32 | var errorInfo = new CompilationErrorInfo(id, level, stage, messageTemplate); 33 | m_errorInfoStore.Add(errorInfo); 34 | } 35 | 36 | public CompilationErrorInfo GetErrorInfo(int id) 37 | { 38 | return m_errorInfoStore[id]; 39 | } 40 | 41 | public bool ContainsErrorDefinition(int id) 42 | { 43 | return m_errorInfoStore.Contains(id); 44 | } 45 | 46 | public CompilationErrorList CreateErrorList() 47 | { 48 | return new CompilationErrorList(this); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/CompilationStage.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers 18 | { 19 | public enum CompilationStage 20 | { 21 | None, 22 | PreProcessing, 23 | Scanning, 24 | Parsing, 25 | SemanticAnalysis, 26 | CodeGeneration, 27 | PostProcessing, 28 | Other 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("VBF.Compilers.Common")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Ninputer")] 27 | [assembly: AssemblyProduct("VBF.Compilers")] 28 | [assembly: AssemblyCopyright("Copyright © 2012 Fan Shi")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("86ef673d-e3e2-4753-a2d6-11fec0571a4b")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion("1.0.0.0")] 51 | [assembly: AssemblyFileVersion("1.0.0.0")] 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/RevertPoint.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers 18 | { 19 | public struct RevertPoint 20 | { 21 | internal RevertPoint(int key, int offset, SourceLocation lastLocation, SourceLocation location) 22 | : this() 23 | { 24 | Key = key; 25 | Offset = offset; 26 | LastLocation = lastLocation; 27 | Location = location; 28 | } 29 | 30 | internal int Offset { get; private set; } 31 | internal int Key { get; private set; } 32 | public SourceLocation LastLocation { get; private set; } 33 | internal SourceLocation Location { get; private set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/RevertPointCollection.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.ObjectModel; 18 | 19 | namespace VBF.Compilers 20 | { 21 | class RevertPointCollection : KeyedCollection 22 | { 23 | protected override int GetKeyForItem(RevertPoint item) 24 | { 25 | return item.Key; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Common/SourceSpan.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers 20 | { 21 | public class SourceSpan : IEquatable 22 | { 23 | private readonly SourceLocation m_endLocation; 24 | private readonly SourceLocation m_startLocation; 25 | 26 | public SourceSpan(SourceLocation startLocation, SourceLocation endLocation) 27 | { 28 | m_startLocation = startLocation; 29 | m_endLocation = endLocation; 30 | } 31 | 32 | public SourceLocation StartLocation 33 | { 34 | get { return m_startLocation; } 35 | } 36 | 37 | public SourceLocation EndLocation 38 | { 39 | get { return m_endLocation; } 40 | } 41 | 42 | public bool Equals(SourceSpan other) 43 | { 44 | if (other == null) 45 | { 46 | return false; 47 | } 48 | 49 | return m_startLocation.Equals(other.m_startLocation) && 50 | m_endLocation.Equals(other.m_endLocation); 51 | } 52 | 53 | public override bool Equals(object obj) 54 | { 55 | return Equals(obj as SourceSpan); 56 | } 57 | 58 | public override int GetHashCode() 59 | { 60 | return (m_endLocation.GetHashCode() << 16) ^ m_startLocation.GetHashCode(); 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/AssignInstruction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class AssignInstruction : Instruction 20 | { 21 | public Variable Target { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/BinaryExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public enum BinaryOperator 20 | { 21 | Unknown, 22 | Add, 23 | Substract, 24 | Multiply, 25 | Divide, 26 | Modulo, 27 | Min, 28 | Max, 29 | Member, 30 | DereferenceMember 31 | } 32 | 33 | public class BinaryExpression : Expression 34 | { 35 | public Operand Operand1 { get; set; } 36 | public Operand Operand2 { get; set; } 37 | 38 | public BinaryOperator Operator { get; set; } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/ConstValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class ConstValue : Operand 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/ConvertExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class ConvertExpression 20 | { 21 | public Operand Operand { get; set; } 22 | public PrimaryType Type { get; set; } 23 | 24 | void Foo() 25 | { 26 | 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/Expression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public abstract class Expression 20 | { 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/GotoInstruction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class GotoInstruction : Instruction 20 | { 21 | public Label Location { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/Instruction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public abstract class Instruction 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/Label.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class Label 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/Operand.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public abstract class Operand 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/PrimaryType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public enum PrimaryType 20 | { 21 | Pointer, 22 | UInt8, 23 | Int32, 24 | Int64, 25 | UInt32, 26 | UInt64, 27 | Single, 28 | Double, 29 | Boolean 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("VBF.Compilers.Intermediate")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Ninputer")] 27 | [assembly: AssemblyProduct("VBF.Compilers")] 28 | [assembly: AssemblyCopyright("Copyright © 2012 Fan Shi")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("665b923c-d1b5-492d-a062-b15927dca6a7")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion("1.0.0.0")] 51 | [assembly: AssemblyFileVersion("1.0.0.0")] 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/ReceiveInstruction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class ReceiveInstruction : Instruction 20 | { 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/TreeLang.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class TreeLang 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/UnaryExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public enum UnaryOperator 20 | { 21 | Unknown, 22 | Minus, 23 | Address, 24 | Dereference 25 | } 26 | public class UnaryExpression : Expression 27 | { 28 | public Operand Operand { get; set; } 29 | public UnaryOperator Operator { get; set; } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Intermediate/Variable.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Intermediate 18 | { 19 | public class Variable : Operand 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/AlternationParser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers.Combinators 18 | { 19 | public class AlternationParser : Parser 20 | { 21 | public AlternationParser(Parser parser1, Parser parser2) 22 | { 23 | CodeContract.RequiresArgumentNotNull(parser1, "parser1"); 24 | CodeContract.RequiresArgumentNotNull(parser2, "parser2"); 25 | 26 | Parser1 = parser1; 27 | Parser2 = parser2; 28 | } 29 | 30 | public Parser Parser1 { get; private set; } 31 | public Parser Parser2 { get; private set; } 32 | 33 | public override ParserFunc BuildParser(Future future) 34 | { 35 | return (scanner, context) => 36 | { 37 | var s1 = scanner; 38 | var s2 = scanner.Fork(); 39 | 40 | return context.ChooseBest(Parser1.BuildParser(future)(s1, context), Parser2.BuildParser(future)(s2, context)); 41 | }; 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/AnyTokenParser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.Compilers.Parsers.Combinators 20 | { 21 | public class AnyTokenParser : Parser 22 | { 23 | public override ParserFunc BuildParser(Future future) 24 | { 25 | return (scanner, context) => 26 | { 27 | var l = scanner.Read(); 28 | return context.StepResult(0, () => future(l)(scanner, context)); 29 | }; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/ConcatenationParser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers.Combinators 20 | { 21 | public class ConcatenationParser : Parser 22 | { 23 | public ConcatenationParser(Parser parserLeft, Func> parserRightSelector, Func selector) 24 | { 25 | CodeContract.RequiresArgumentNotNull(parserLeft, "parserLeft"); 26 | CodeContract.RequiresArgumentNotNull(parserRightSelector, "parserRightSelector"); 27 | CodeContract.RequiresArgumentNotNull(selector, "selector"); 28 | 29 | ParserLeft = parserLeft; 30 | ParserRightSelector = parserRightSelector; 31 | Selector = selector; 32 | } 33 | 34 | public Parser ParserLeft { get; private set; } 35 | public Func> ParserRightSelector { get; private set; } 36 | public Func Selector { get; private set; } 37 | 38 | public override ParserFunc BuildParser(Future future) 39 | { 40 | return (scanner, context) => 41 | ParserLeft.BuildParser( 42 | left => ParserRightSelector(left).BuildParser( 43 | right => future(Selector(left, right))))(scanner, context); 44 | } 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/ConvertHelper.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Linq.Expressions; 19 | 20 | namespace VBF.Compilers.Parsers.Combinators 21 | { 22 | class ConvertHelper 23 | { 24 | private static Func s_castFunc; 25 | 26 | static ConvertHelper() 27 | { 28 | var source = Expression.Parameter(typeof(TFrom), "source"); 29 | s_castFunc = Expression.Lambda>(Expression.Convert(source, typeof(TTo)), source).Compile(); 30 | } 31 | 32 | public static TTo Convert(TFrom source) 33 | { 34 | return s_castFunc(source); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/EndOfStreamParser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.Compilers.Parsers.Combinators 20 | { 21 | public class EndOfStreamParser : Parser 22 | { 23 | public override ParserFunc BuildParser(Future future) 24 | { 25 | ParserFunc scan = null; 26 | scan = (scanner, context) => 27 | { 28 | 29 | var l = scanner.Read(); 30 | 31 | if (l.IsEndOfStream) 32 | { 33 | return context.StepResult(0, () => future(l)(scanner, context)); 34 | } 35 | ErrorCorrection deleteCorrection = new DeletedErrorCorrection(l); 36 | return context.StepResult(1, () => scan(scanner, context), deleteCorrection); //delete to recover 37 | }; 38 | 39 | return scan; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/MappingParser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers.Combinators 20 | { 21 | public class MappingParser : Parser 22 | { 23 | public MappingParser(Parser sourceParser, Func selector) 24 | { 25 | CodeContract.RequiresArgumentNotNull(sourceParser, "sourceParser"); 26 | CodeContract.RequiresArgumentNotNull(selector, "selector"); 27 | 28 | SourceParser = sourceParser; 29 | Selector = selector; 30 | } 31 | 32 | public Parser SourceParser { get; private set; } 33 | public Func Selector { get; private set; } 34 | 35 | public override ParserFunc BuildParser(Future future) 36 | { 37 | return (scanner, context) => SourceParser.BuildParser(vsource => future(Selector(vsource)))(scanner, context); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/Parser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.Compilers.Parsers.Combinators 20 | { 21 | public delegate Result ParserFunc(ForkableScanner scanner, ParserContext context); 22 | public delegate ParserFunc Future(T value); 23 | 24 | public abstract class Parser 25 | { 26 | public abstract ParserFunc BuildParser(Future future); 27 | 28 | public static Parser operator |(Parser p1, Parser p2) 29 | { 30 | return new AlternationParser(p1, p2); 31 | } 32 | 33 | public Parser Convert() 34 | { 35 | CodeContract.RequiresArgumentNotNull(this, "parser"); 36 | 37 | return new MappingParser(this, ConvertHelper.Convert); 38 | } 39 | 40 | public Parser TryCast() 41 | where TResult : class 42 | { 43 | CodeContract.RequiresArgumentNotNull(this, "parser"); 44 | 45 | return new MappingParser(this, t => t as TResult); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/ParserReference.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers.Combinators 18 | { 19 | public class ParserReference : Parser 20 | { 21 | public Parser Reference { get; set; } 22 | 23 | public override ParserFunc BuildParser(Future future) 24 | { 25 | return Reference.BuildParser(future); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/ParserRunner.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Diagnostics; 18 | using VBF.Compilers.Scanners; 19 | 20 | namespace VBF.Compilers.Parsers.Combinators 21 | { 22 | public class ParserRunner 23 | { 24 | private ParserFunc m_runner; 25 | 26 | public ParserRunner(Parser parser, ParserContext context) 27 | { 28 | CodeContract.RequiresArgumentNotNull(parser, "parser"); 29 | CodeContract.RequiresArgumentNotNull(context, "context"); 30 | 31 | m_runner = parser.BuildParser(FinalFuture); 32 | Debug.Assert(m_runner != null); 33 | Context = context; 34 | } 35 | 36 | public ParserContext Context { get; private set; } 37 | 38 | public T Run(ForkableScanner scanner) 39 | { 40 | var result = m_runner(scanner, Context); 41 | return result.GetResult(Context); 42 | } 43 | 44 | private ParserFunc FinalFuture(T value) 45 | { 46 | return (scanner, context) => context.StopResult(value); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("VBF.Compilers.Parsers.Combinators")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Ninputer")] 27 | [assembly: AssemblyProduct("VBF.Compilers")] 28 | [assembly: AssemblyCopyright("Copyright © 2012 Fan Shi")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("08715c59-21fa-4326-8ae8-2a4f31c28dbe")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion("1.0.0.0")] 51 | [assembly: AssemblyFileVersion("1.0.0.0")] 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/RepeatParserListNode.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Parsers.Combinators 21 | { 22 | sealed class RepeatParserListNode : IEnumerable 23 | { 24 | public RepeatParserListNode(T value, RepeatParserListNode next) 25 | { 26 | Value = value; 27 | Next = next; 28 | } 29 | 30 | public RepeatParserListNode() : this(default(T), null) { } 31 | public T Value { get; private set; } 32 | public RepeatParserListNode Next { get; private set; } 33 | 34 | 35 | public IEnumerator GetEnumerator() 36 | { 37 | RepeatParserListNode current = this; 38 | 39 | while (current.Next != null) 40 | { 41 | yield return current.Value; 42 | 43 | current = current.Next; 44 | } 45 | } 46 | 47 | IEnumerator IEnumerable.GetEnumerator() 48 | { 49 | return GetEnumerator(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers.Combinators/SucceedParser.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers.Combinators 18 | { 19 | public class SucceedParser : Parser 20 | { 21 | public SucceedParser(T value) 22 | { 23 | Value = value; 24 | } 25 | 26 | public T Value { get; private set; } 27 | 28 | public override ParserFunc BuildParser(Future future) 29 | { 30 | return (scanner, context) => future(Value)(scanner, context); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/AlternationProduction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers 20 | { 21 | public class AlternationProduction : ProductionBase 22 | { 23 | public AlternationProduction(ProductionBase production1, ProductionBase production2) 24 | { 25 | CodeContract.RequiresArgumentNotNull(production1, "production1"); 26 | CodeContract.RequiresArgumentNotNull(production2, "production2"); 27 | 28 | Production1 = production1; 29 | Production2 = production2; 30 | } 31 | 32 | public ProductionBase Production1 { get; private set; } 33 | public ProductionBase Production2 { get; private set; } 34 | 35 | public override string DebugName 36 | { 37 | get 38 | { 39 | return "A" + DebugNameSuffix; 40 | } 41 | } 42 | 43 | public override TResult Accept(IProductionVisitor visitor, TArg arg) 44 | { 45 | return visitor.VisitAlternation(this, arg); 46 | } 47 | 48 | public override string ToString() 49 | { 50 | return String.Format("{0} ::= {1} | {2}", DebugName, Production1.DebugName, Production2.DebugName); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/AmbiguityAggregator.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers 20 | { 21 | internal abstract class AmbiguityAggregator 22 | { 23 | protected AmbiguityAggregator(int productionIndex) 24 | { 25 | ProductionIndex = productionIndex; 26 | } 27 | 28 | public int ProductionIndex { get; private set; } 29 | 30 | public abstract object Aggregate(object result1, object result2); 31 | } 32 | 33 | internal class AmbiguityAggregator : AmbiguityAggregator 34 | { 35 | private Func m_aggregator; 36 | 37 | public AmbiguityAggregator(int productionIndex, Func aggregator) 38 | : base(productionIndex) 39 | { 40 | m_aggregator = aggregator; 41 | } 42 | 43 | public override object Aggregate(object result1, object result2) 44 | { 45 | return m_aggregator((T)result1, (T)result2); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/DefaultValueContainer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using VBF.Compilers.Scanners; 7 | 8 | namespace VBF.Compilers.Parsers 9 | { 10 | class DefaultValueContainer 11 | { 12 | static DefaultValueContainer() 13 | { 14 | if (typeof(T) == typeof(Lexeme)) 15 | { 16 | DefaultValue = (T)(object)Lexeme.CreateEmptyLexeme(); 17 | } 18 | else 19 | { 20 | DefaultValue = default(T); 21 | } 22 | } 23 | 24 | public static readonly T DefaultValue; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/EmptyProduction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers 18 | { 19 | public class EmptyProduction : ProductionBase 20 | { 21 | public EmptyProduction(T value) 22 | { 23 | Value = value; 24 | } 25 | 26 | public T Value { get; private set; } 27 | 28 | public override string DebugName 29 | { 30 | get 31 | { 32 | return "E" + DebugNameSuffix; 33 | } 34 | } 35 | 36 | public override TResult Accept(IProductionVisitor visitor, TArg argument) 37 | { 38 | return visitor.VisitEmpty(this, argument); 39 | } 40 | 41 | public override string ToString() 42 | { 43 | return DebugName + " ::= ε"; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/EndOfStream.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.Compilers.Parsers 20 | { 21 | public class EndOfStream : ProductionBase 22 | { 23 | private static EndOfStream s_instance = new EndOfStream(); 24 | 25 | private EndOfStream() 26 | { 27 | 28 | } 29 | 30 | public static EndOfStream Instance 31 | { 32 | get 33 | { 34 | return s_instance; 35 | } 36 | } 37 | 38 | public override bool IsTerminal 39 | { 40 | get 41 | { 42 | return true; 43 | } 44 | } 45 | 46 | public override bool IsEos 47 | { 48 | get 49 | { 50 | return true; 51 | } 52 | } 53 | 54 | public override string DebugName 55 | { 56 | get 57 | { 58 | return "$"; 59 | } 60 | } 61 | 62 | public override TResult Accept(IProductionVisitor visitor, TArg argument) 63 | { 64 | return visitor.VisitEndOfStream(this, argument); 65 | } 66 | 67 | public override bool Equals(object obj) 68 | { 69 | var otherEos = obj as EndOfStream; 70 | 71 | if (otherEos != null) 72 | { 73 | return true; 74 | } 75 | 76 | return false; 77 | } 78 | 79 | public override int GetHashCode() 80 | { 81 | return -3177; 82 | } 83 | 84 | public override string ToString() 85 | { 86 | return "$"; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/ErrorRecord.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers 20 | { 21 | internal class ErrorRecord 22 | { 23 | public readonly int? ErrorId; 24 | public readonly SourceSpan ErrorPosition; 25 | public Object ErrorArgument; 26 | public Object ErrorArgument2; 27 | 28 | public ErrorRecord(int? id, SourceSpan position) 29 | { 30 | ErrorId = id; 31 | ErrorPosition = position; 32 | } 33 | 34 | public override bool Equals(object obj) 35 | { 36 | ErrorRecord other = obj as ErrorRecord; 37 | if (other == null) 38 | { 39 | return false; 40 | } 41 | 42 | return ErrorId == other.ErrorId && 43 | Equals(ErrorPosition, other.ErrorPosition) && 44 | Equals(ErrorArgument, other.ErrorArgument) && 45 | Equals(ErrorArgument2, other.ErrorArgument2); 46 | } 47 | 48 | public override int GetHashCode() 49 | { 50 | unchecked 51 | { 52 | return (ErrorId ?? -1) ^ ErrorPosition.GetHashCode(); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Generator/LR0Edge.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers.Generator 20 | { 21 | public struct LR0Edge : IEquatable 22 | { 23 | private int m_sourceStateIndex; 24 | private int m_symbolIndex; 25 | private int m_targetStateIndex; 26 | 27 | internal LR0Edge(int sourceStateIndex, int symbolIndex, int targetStateIndex) 28 | { 29 | m_sourceStateIndex = sourceStateIndex; 30 | m_symbolIndex = symbolIndex; 31 | m_targetStateIndex = targetStateIndex; 32 | } 33 | 34 | public int SymbolIndex 35 | { 36 | get 37 | { 38 | return m_symbolIndex; 39 | } 40 | } 41 | 42 | public int SourceStateIndex 43 | { 44 | get 45 | { 46 | return m_sourceStateIndex; 47 | } 48 | } 49 | 50 | public int TargetStateIndex 51 | { 52 | get 53 | { 54 | return m_targetStateIndex; 55 | } 56 | } 57 | 58 | public bool Equals(LR0Edge other) 59 | { 60 | return m_symbolIndex == other.m_symbolIndex && 61 | m_targetStateIndex == other.m_targetStateIndex && 62 | m_sourceStateIndex == other.m_sourceStateIndex; 63 | } 64 | 65 | public override bool Equals(object obj) 66 | { 67 | if (obj == null) 68 | { 69 | return false; 70 | } 71 | 72 | LR0Edge other = (LR0Edge)obj; 73 | 74 | return Equals(other); 75 | } 76 | 77 | public override int GetHashCode() 78 | { 79 | return (m_sourceStateIndex << 24) ^ (m_symbolIndex << 12) ^ m_targetStateIndex; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Generator/LR0Item.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers.Generator 20 | { 21 | public struct LR0Item : IEquatable 22 | { 23 | public LR0Item(int productionIndex, int dotLocation) : this() 24 | { 25 | ProductionIndex = productionIndex; 26 | DotLocation = dotLocation; 27 | } 28 | 29 | public int ProductionIndex { get; set; } 30 | public int DotLocation { get; set; } 31 | 32 | public bool Equals(LR0Item other) 33 | { 34 | return ProductionIndex == other.ProductionIndex && DotLocation == other.DotLocation; 35 | } 36 | 37 | public override bool Equals(object obj) 38 | { 39 | if (obj == null) 40 | { 41 | return false; 42 | } 43 | 44 | return Equals((LR0Item)obj); 45 | } 46 | 47 | public override int GetHashCode() 48 | { 49 | return (DotLocation << 16) ^ ProductionIndex; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Generator/ProductionInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | 19 | namespace VBF.Compilers.Parsers.Generator 20 | { 21 | public class ProductionInfo 22 | { 23 | public ProductionInfo() 24 | { 25 | First = new HashSet(); 26 | Follow = new HashSet(); 27 | IsNullable = false; 28 | } 29 | 30 | public ISet First { get; private set; } 31 | public ISet Follow { get; private set; } 32 | public bool IsNullable { get; internal set; } 33 | 34 | internal int Index { get; set; } 35 | internal int SymbolCount { get; set; } 36 | internal int NonTerminalIndex { get; set; } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Generator/ProductionInfoManager.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | 19 | namespace VBF.Compilers.Parsers.Generator 20 | { 21 | public class ProductionInfoManager 22 | { 23 | private IProduction[] m_productions; 24 | 25 | public ProductionInfoManager(IProduction root) 26 | { 27 | CodeContract.RequiresArgumentNotNull(root, "root"); 28 | 29 | var aggregator = new ProductionAggregationVisitor(); 30 | var productions = root.Accept(aggregator, new List()); 31 | 32 | m_productions = productions.ToArray(); 33 | RootProduction = root; 34 | 35 | var ffVisitor = new FirstFollowVisitor(); 36 | 37 | bool isChanged; 38 | 39 | do 40 | { 41 | isChanged = false; 42 | 43 | foreach (var p in productions) 44 | { 45 | isChanged = p.Accept(ffVisitor, isChanged); 46 | } 47 | 48 | } while (isChanged); 49 | } 50 | 51 | public IProduction RootProduction { get; private set; } 52 | 53 | public IReadOnlyList Productions 54 | { 55 | get 56 | { 57 | return m_productions; 58 | } 59 | } 60 | 61 | public ProductionInfo GetInfo(IProduction production) 62 | { 63 | return (production as ProductionBase).Info; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Generator/ReduceAction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers.Generator 18 | { 19 | public class ReduceAction 20 | { 21 | private IProduction m_reduceProduction; 22 | private IProduction m_reduceTerminal; 23 | 24 | internal ReduceAction(IProduction reduceTerminl, IProduction reduceProduction) 25 | { 26 | this.m_reduceTerminal = reduceTerminl; 27 | this.m_reduceProduction = reduceProduction; 28 | } 29 | 30 | public IProduction ReduceTerminal 31 | { 32 | get 33 | { 34 | return m_reduceTerminal; 35 | } 36 | } 37 | 38 | public IProduction ReduceProduction 39 | { 40 | get 41 | { 42 | return m_reduceProduction; 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Generator/SymbolConversion.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers.Generator 18 | { 19 | class SymbolConversion 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/IProduction.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers 18 | { 19 | public interface IProduction 20 | { 21 | bool IsTerminal { get; } 22 | bool IsEos { get; } 23 | bool AggregatesAmbiguities { get; } 24 | TResult Accept(IProductionVisitor visitor, TArg argument); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/IProductionVisitor.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers 18 | { 19 | public interface IProductionVisitor 20 | { 21 | TResult VisitTerminal(Terminal terminal, TArg argument); 22 | 23 | TResult VisitMapping(MappingProduction mappingProduction, TArg argument); 24 | 25 | TResult VisitEndOfStream(EndOfStream endOfStream, TArg argument); 26 | 27 | TResult VisitEmpty(EmptyProduction emptyProduction, TArg argument); 28 | 29 | TResult VisitAlternation(AlternationProduction alternationProduction, TArg argument); 30 | 31 | TResult VisitConcatenation(ConcatenationProduction concatenationProduction, TArg argument); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/PanicRecoverException.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Parsers 21 | { 22 | class PanicRecoverException : Exception 23 | { 24 | public PanicRecoverException(ISet follow) 25 | { 26 | PossibleFollow = follow; 27 | } 28 | 29 | public ISet PossibleFollow { get; private set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/ParsingFailureException.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Parsers 20 | { 21 | public class ParsingFailureException : Exception 22 | { 23 | public ParsingFailureException(string message) :base(message) 24 | { 25 | 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("VBF.Compilers.Parsers")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Ninputer")] 27 | [assembly: AssemblyProduct("VBF.Compilers")] 28 | [assembly: AssemblyCopyright("Copyright © 2012 Fan Shi")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("c4fc45c5-c087-4873-916b-b4f0a10d8685")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion("1.0.0.0")] 51 | [assembly: AssemblyFileVersion("1.0.0.0")] 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/RepeatParserListNode.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Parsers 21 | { 22 | sealed class RepeatParserListNode : IEnumerable 23 | { 24 | public RepeatParserListNode(T value, RepeatParserListNode next) 25 | { 26 | Value = value; 27 | Next = next; 28 | } 29 | 30 | public RepeatParserListNode() : this(default(T), null) { } 31 | public T Value { get; private set; } 32 | public RepeatParserListNode Next { get; private set; } 33 | 34 | 35 | public IEnumerator GetEnumerator() 36 | { 37 | RepeatParserListNode current = this; 38 | 39 | while (current.Next != null) 40 | { 41 | yield return current.Value; 42 | 43 | current = current.Next; 44 | } 45 | } 46 | 47 | IEnumerator IEnumerable.GetEnumerator() 48 | { 49 | return GetEnumerator(); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/ResultInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers 18 | { 19 | public class ResultInfo 20 | { 21 | internal ResultInfo(int errorCount) 22 | { 23 | ErrorCount = errorCount; 24 | } 25 | 26 | public int ErrorCount { get; private set; } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/SetHelpers.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | 19 | namespace VBF.Compilers.Parsers 20 | { 21 | internal static class SetHelpers 22 | { 23 | internal static bool UnionCheck(this ISet set, IEnumerable toUnion) 24 | { 25 | bool changed = false; 26 | 27 | foreach (var item in toUnion) 28 | { 29 | changed = set.Add(item) || changed; 30 | } 31 | 32 | return changed; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/StackNode.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers 18 | { 19 | internal class StackNode 20 | { 21 | internal readonly StackNode PrevNode; 22 | internal readonly int StateIndex; 23 | internal object ReducedValue; 24 | 25 | public StackNode(int stateIndex, StackNode prev, object value) 26 | { 27 | StateIndex = stateIndex; 28 | PrevNode = prev; 29 | ReducedValue = value; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Parsers/SyntaxErrors.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Parsers 18 | { 19 | public class SyntaxErrors 20 | { 21 | public int TokenMissingId { get; set; } 22 | public int TokenUnexpectedId { get; set; } 23 | public int TokenMistakeId { get; set; } 24 | public int OtherErrorId { get; set; } 25 | public int LexicalErrorId { get; set; } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/AlternationCharSetExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Collections.ObjectModel; 20 | 21 | namespace VBF.Compilers.Scanners 22 | { 23 | public sealed class AlternationCharSetExpression : RegularExpression 24 | { 25 | private List m_charSet; 26 | 27 | public AlternationCharSetExpression(IEnumerable charset) 28 | : base(RegularExpressionType.AlternationCharSet) 29 | { 30 | m_charSet = new List(charset); 31 | } 32 | 33 | public new ReadOnlyCollection CharSet 34 | { 35 | get 36 | { 37 | return m_charSet.AsReadOnly(); 38 | } 39 | } 40 | 41 | public override string ToString() 42 | { 43 | if (m_charSet.Count == 0) 44 | { 45 | return String.Empty; 46 | } 47 | 48 | return '[' + new String(m_charSet.ToArray()) + ']'; 49 | } 50 | 51 | internal override Func>[] GetCompactableCharSets() 52 | { 53 | return new Func>[] { () => new HashSet(m_charSet) }; 54 | } 55 | 56 | internal override HashSet GetUncompactableCharSet() 57 | { 58 | return new HashSet(); 59 | } 60 | 61 | internal override T Accept(RegularExpressionConverter converter) 62 | { 63 | return converter.ConvertAlternationCharSet(this); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/AlternationExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Linq; 20 | 21 | namespace VBF.Compilers.Scanners 22 | { 23 | /// 24 | /// Represents an alternation of two regular expressions 25 | /// 26 | public sealed class AlternationExpression : RegularExpression 27 | { 28 | public AlternationExpression(RegularExpression expression1, RegularExpression expression2) 29 | : base(RegularExpressionType.Alternation) 30 | { 31 | CodeContract.RequiresArgumentNotNull(expression1, "expression1"); 32 | CodeContract.RequiresArgumentNotNull(expression2, "expression2"); 33 | 34 | Expression1 = expression1; 35 | Expression2 = expression2; 36 | } 37 | 38 | public RegularExpression Expression1 { get; private set; } 39 | public RegularExpression Expression2 { get; private set; } 40 | 41 | public override string ToString() 42 | { 43 | return '(' + Expression1.ToString() + '|' + Expression2.ToString() +')'; 44 | } 45 | 46 | internal override Func>[] GetCompactableCharSets() 47 | { 48 | return Expression1.GetCompactableCharSets().Union(Expression2.GetCompactableCharSets()).ToArray(); 49 | } 50 | 51 | internal override HashSet GetUncompactableCharSet() 52 | { 53 | var result = Expression1.GetUncompactableCharSet(); 54 | result.UnionWith(Expression2.GetUncompactableCharSet()); 55 | 56 | return result; 57 | } 58 | 59 | internal override T Accept(RegularExpressionConverter converter) 60 | { 61 | return converter.ConvertAlternation(this); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/ConcatenationExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Linq; 20 | 21 | namespace VBF.Compilers.Scanners 22 | { 23 | /// 24 | /// Represents a concatenation of two regular expressions 25 | /// 26 | public sealed class ConcatenationExpression : RegularExpression 27 | { 28 | public ConcatenationExpression(RegularExpression left, RegularExpression right) 29 | : base(RegularExpressionType.Concatenation) 30 | { 31 | CodeContract.RequiresArgumentNotNull(left, "left"); 32 | CodeContract.RequiresArgumentNotNull(right, "right"); 33 | 34 | Left = left; 35 | Right = right; 36 | } 37 | 38 | public RegularExpression Left { get; private set; } 39 | public RegularExpression Right { get; private set; } 40 | 41 | public override string ToString() 42 | { 43 | return Left.ToString() + Right.ToString(); 44 | } 45 | 46 | internal override Func>[] GetCompactableCharSets() 47 | { 48 | return Left.GetCompactableCharSets().Union(Right.GetCompactableCharSets()).ToArray(); 49 | } 50 | 51 | internal override HashSet GetUncompactableCharSet() 52 | { 53 | var result = Left.GetUncompactableCharSet(); 54 | result.UnionWith(Right.GetUncompactableCharSet()); 55 | 56 | return result; 57 | } 58 | 59 | internal override T Accept(RegularExpressionConverter converter) 60 | { 61 | return converter.ConvertConcatenation(this); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/EmptyExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Scanners 21 | { 22 | /// 23 | /// Represents a regular expression accepts an empty set 24 | /// 25 | public sealed class EmptyExpression : RegularExpression 26 | { 27 | public static readonly EmptyExpression Instance = new EmptyExpression(); 28 | private EmptyExpression() : base(RegularExpressionType.Empty) { } 29 | 30 | public override string ToString() 31 | { 32 | return "ε"; 33 | } 34 | 35 | internal override Func>[] GetCompactableCharSets() 36 | { 37 | return new Func>[0]; 38 | } 39 | 40 | internal override HashSet GetUncompactableCharSet() 41 | { 42 | return new HashSet(); 43 | } 44 | 45 | internal override T Accept(RegularExpressionConverter converter) 46 | { 47 | return converter.ConvertEmpty(this); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/FiniteAutomationEngine.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Diagnostics; 18 | 19 | namespace VBF.Compilers.Scanners 20 | { 21 | class FiniteAutomationEngine 22 | { 23 | private ushort[] m_charClassTable; 24 | private int m_currentState; 25 | private int[][] m_transitionTable; 26 | 27 | public FiniteAutomationEngine(int[][] transitionTable, ushort[] charClassTable) 28 | { 29 | m_transitionTable = transitionTable; 30 | m_charClassTable = charClassTable; 31 | 32 | Debug.Assert(m_transitionTable.Length > 0); 33 | 34 | m_currentState = 1; 35 | } 36 | 37 | public int CurrentState 38 | { 39 | get 40 | { 41 | return m_currentState; 42 | } 43 | } 44 | 45 | public bool IsAtStoppedState 46 | { 47 | get 48 | { 49 | return m_currentState == 0; 50 | } 51 | } 52 | 53 | public void Reset() 54 | { 55 | m_currentState = 1; 56 | } 57 | 58 | public void Input(char c) 59 | { 60 | int[] transitions = m_transitionTable[m_currentState]; 61 | //find out which is the next state 62 | ushort charClass = m_charClassTable[c]; 63 | int nextState = transitions[charClass]; 64 | 65 | //go to next state 66 | m_currentState = nextState; 67 | } 68 | 69 | public void InputString(string str) 70 | { 71 | for (int i = 0; i < str.Length; i++) 72 | { 73 | Input(str[i]); 74 | } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/ForkableScannerCore.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Scanners 18 | { 19 | class ForkableScannerCore 20 | { 21 | internal readonly CacheQueue LookAheadQueue; 22 | internal readonly Scanner MasterScanner; 23 | 24 | internal ForkableScannerCore(Scanner masterScanner) 25 | { 26 | MasterScanner = masterScanner; 27 | LookAheadQueue = new CacheQueue(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Generator/DFAEdge.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Scanners.Generator 18 | { 19 | public struct DFAEdge 20 | { 21 | public DFAEdge(int symbol, DFAState targetState) 22 | : this() 23 | { 24 | Symbol = symbol; 25 | TargetState = targetState; 26 | } 27 | 28 | public int Symbol { get; private set; } 29 | public DFAState TargetState { get; private set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Generator/DFAState.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | 20 | namespace VBF.Compilers.Scanners.Generator 21 | { 22 | public class DFAState 23 | { 24 | private HashSet m_nfaStateSet; 25 | private List m_outEdges; 26 | 27 | internal DFAState() 28 | { 29 | m_outEdges = new List(); 30 | m_nfaStateSet = new HashSet(); 31 | } 32 | 33 | public int Index { get; internal set; } 34 | 35 | public ReadOnlyCollection OutEdges 36 | { 37 | get 38 | { 39 | return m_outEdges.AsReadOnly(); 40 | } 41 | } 42 | 43 | public ISet NFAStateSet 44 | { 45 | get 46 | { 47 | return m_nfaStateSet; 48 | } 49 | } 50 | 51 | internal void AddEdge(DFAEdge edge) 52 | { 53 | CodeContract.RequiresArgumentNotNull(edge, "edge"); 54 | 55 | m_outEdges.Add(edge); 56 | } 57 | 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Generator/NFAEdge.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Scanners.Generator 18 | { 19 | public struct NFAEdge 20 | { 21 | public NFAEdge(int symbol, NFAState targetState) 22 | : this() 23 | { 24 | Symbol = symbol; 25 | TargetState = targetState; 26 | } 27 | 28 | public NFAEdge(NFAState targetState) 29 | : this() 30 | { 31 | TargetState = targetState; 32 | } 33 | 34 | public int? Symbol { get; private set; } 35 | public NFAState TargetState { get; private set; } 36 | 37 | public bool IsEmpty 38 | { 39 | get 40 | { 41 | return !Symbol.HasValue; 42 | } 43 | } 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Generator/NFAModel.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | 20 | namespace VBF.Compilers.Scanners.Generator 21 | { 22 | public class NFAModel 23 | { 24 | private ReadOnlyCollection m_readonlyStates; 25 | private List m_states; 26 | 27 | internal NFAModel() 28 | { 29 | m_states = new List(); 30 | m_readonlyStates = new ReadOnlyCollection(m_states); 31 | } 32 | 33 | public NFAState TailState { get; internal set; } 34 | public NFAEdge EntryEdge { get; internal set; } 35 | 36 | public ReadOnlyCollection States 37 | { 38 | get 39 | { 40 | return m_readonlyStates; 41 | } 42 | } 43 | 44 | internal void AddState(NFAState state) 45 | { 46 | m_states.Add(state); 47 | state.Index = m_states.Count - 1; 48 | } 49 | 50 | internal void AddStates(IEnumerable states) 51 | { 52 | foreach (var s in states) 53 | { 54 | AddState(s); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Generator/NFAState.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | using System.Diagnostics; 20 | 21 | namespace VBF.Compilers.Scanners.Generator 22 | { 23 | [DebuggerDisplay("State #{Index}")] 24 | public class NFAState 25 | { 26 | private List m_outEdges; 27 | private ReadOnlyCollection m_readonlyOutEdges; 28 | 29 | internal NFAState() 30 | { 31 | m_outEdges = new List(); 32 | m_readonlyOutEdges = new ReadOnlyCollection(m_outEdges); 33 | 34 | //default value -1 means no token is bound with this state 35 | TokenIndex = -1; 36 | } 37 | 38 | public int Index { get; internal set; } 39 | internal int TokenIndex { get; set; } 40 | 41 | public ReadOnlyCollection OutEdges 42 | { 43 | get 44 | { 45 | return m_readonlyOutEdges; 46 | } 47 | } 48 | 49 | internal void AddEmptyEdgeTo(NFAState targetState) 50 | { 51 | CodeContract.RequiresArgumentNotNull(targetState, "targetState"); 52 | 53 | m_outEdges.Add(new NFAEdge(targetState)); 54 | } 55 | 56 | internal void AddEdge(NFAEdge edge) 57 | { 58 | CodeContract.RequiresArgumentNotNull(edge, "edge"); 59 | 60 | m_outEdges.Add(edge); 61 | } 62 | 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/HistoryList.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Scanners 21 | { 22 | class HistoryList : IReadOnlyList 23 | { 24 | private IReadOnlyList m_fullHistory; 25 | private IReadOnlyList m_valuableHistory; 26 | 27 | public HistoryList(IReadOnlyList fullHistory, IReadOnlyList valuableHistory) 28 | { 29 | m_fullHistory = fullHistory; 30 | m_valuableHistory = valuableHistory; 31 | } 32 | 33 | public Lexeme this[int index] 34 | { 35 | get { return m_fullHistory[m_valuableHistory[index]]; } 36 | } 37 | 38 | public int Count 39 | { 40 | get { return m_valuableHistory.Count; } 41 | } 42 | 43 | public IEnumerator GetEnumerator() 44 | { 45 | for (int i = 0; i < m_valuableHistory.Count; i++) 46 | { 47 | yield return m_fullHistory[m_valuableHistory[i]]; 48 | } 49 | } 50 | 51 | IEnumerator IEnumerable.GetEnumerator() 52 | { 53 | return GetEnumerator(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/KleeneStarExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Scanners 21 | { 22 | public sealed class KleeneStarExpression : RegularExpression 23 | { 24 | public KleeneStarExpression(RegularExpression innerExp) 25 | : base(RegularExpressionType.KleeneStar) 26 | { 27 | CodeContract.RequiresArgumentNotNull(innerExp, "innerExp"); 28 | 29 | InnerExpression = innerExp; 30 | } 31 | 32 | public RegularExpression InnerExpression { get; private set; } 33 | 34 | public override string ToString() 35 | { 36 | return '(' + InnerExpression.ToString() + ")*"; 37 | } 38 | 39 | internal override Func>[] GetCompactableCharSets() 40 | { 41 | return InnerExpression.GetCompactableCharSets(); 42 | } 43 | 44 | internal override HashSet GetUncompactableCharSet() 45 | { 46 | return InnerExpression.GetUncompactableCharSet(); 47 | } 48 | 49 | internal override T Accept(RegularExpressionConverter converter) 50 | { 51 | return converter.ConvertKleeneStar(this); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/LexemeValue.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Scanners 18 | { 19 | public sealed class LexemeValue 20 | { 21 | public LexemeValue(string content, SourceSpan span) 22 | { 23 | CodeContract.RequiresArgumentNotNull(span, "span"); 24 | 25 | Content = content; 26 | Span = span; 27 | } 28 | 29 | public string Content { get; private set; } 30 | public SourceSpan Span { get; private set; } 31 | 32 | public override string ToString() 33 | { 34 | return Content; 35 | } 36 | } 37 | 38 | public static class LexemeExtensions 39 | { 40 | /// 41 | /// Gets the Value of a lexeme if it is not null, otherwise null(Nothing in Visual Basic). 42 | /// 43 | /// The lexeme that the value is going to get from. 44 | /// Value of a lexeme if it is not null, otherwise null(Nothing in Visual Basic). 45 | public static LexemeValue GetValue(this Lexeme lexeme) 46 | { 47 | if (lexeme != null) 48 | { 49 | return lexeme.Value; 50 | } 51 | else 52 | { 53 | return null; 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Lexer.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | 19 | namespace VBF.Compilers.Scanners 20 | { 21 | public class Lexer 22 | { 23 | private List m_tokens; 24 | internal Lexer(Lexicon lexicon, int index) : this(lexicon, index, null) { } 25 | 26 | internal Lexer(Lexicon lexicon, int index, Lexer baseLexer) 27 | { 28 | Children = new List(); 29 | Lexicon = lexicon; 30 | BaseLexer = baseLexer; 31 | m_tokens = new List(); 32 | Index = index; 33 | 34 | if (baseLexer == null) 35 | { 36 | Level = 0; 37 | } 38 | else 39 | { 40 | Level = baseLexer.Level + 1; 41 | baseLexer.Children.Add(this); 42 | } 43 | } 44 | 45 | public Lexicon Lexicon { get; private set; } 46 | public Lexer BaseLexer { get; private set; } 47 | public int Index { get; private set; } 48 | internal int Level { get; private set; } 49 | internal List Children { get; private set; } 50 | 51 | public Token DefineToken(RegularExpression regex, string description) 52 | { 53 | CodeContract.RequiresArgumentNotNull(regex, "regex"); 54 | 55 | int indexInState = m_tokens.Count; 56 | 57 | TokenInfo token = Lexicon.AddToken(regex, this, indexInState, description); 58 | m_tokens.Add(token); 59 | 60 | return token.Tag; 61 | } 62 | 63 | public Token DefineToken(RegularExpression regex) 64 | { 65 | return DefineToken(regex, null); 66 | } 67 | 68 | public Lexer CreateSubLexer() 69 | { 70 | return Lexicon.DefineLexer(this); 71 | } 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("VBF.Compilers.Scanners")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Ninputer")] 27 | [assembly: AssemblyProduct("VBF.Compilers")] 28 | [assembly: AssemblyCopyright("Copyright © 2012 Fan Shi")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("20f666e8-b39e-4fba-a188-201c449f9979")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion("1.0.0.0")] 51 | [assembly: AssemblyFileVersion("1.0.0.0")] 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/RegularExpressionConverter.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Scanners 18 | { 19 | /// 20 | /// Used to convert a regular expression to a value. One must inherit this class to implement custom logic. 21 | /// 22 | /// The target type that the converter converts a regular expression to 23 | public abstract class RegularExpressionConverter 24 | { 25 | protected RegularExpressionConverter() { } 26 | 27 | public T Convert(RegularExpression expression) 28 | { 29 | if (expression == null) 30 | { 31 | return default(T); 32 | } 33 | 34 | return expression.Accept(this); 35 | } 36 | 37 | public abstract T ConvertAlternation(AlternationExpression exp); 38 | 39 | public abstract T ConvertSymbol(SymbolExpression exp); 40 | 41 | public abstract T ConvertEmpty(EmptyExpression exp); 42 | 43 | public abstract T ConvertConcatenation(ConcatenationExpression exp); 44 | 45 | public abstract T ConvertAlternationCharSet(AlternationCharSetExpression exp); 46 | 47 | public abstract T ConvertStringLiteral(StringLiteralExpression exp); 48 | 49 | public abstract T ConvertKleeneStar(KleeneStarExpression exp); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/RegularExpressionType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.Compilers.Scanners 18 | { 19 | public enum RegularExpressionType 20 | { 21 | Empty, 22 | Symbol, 23 | Alternation, 24 | Concatenation, 25 | KleeneStar, 26 | AlternationCharSet, 27 | StringLiteral 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/ScannerException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace VBF.Compilers.Scanners 8 | { 9 | 10 | [Serializable] 11 | public class ScannerException : Exception 12 | { 13 | public ScannerException() { } 14 | public ScannerException(string message) : base(message) { } 15 | public ScannerException(string message, Exception inner) : base(message, inner) { } 16 | protected ScannerException( 17 | System.Runtime.Serialization.SerializationInfo info, 18 | System.Runtime.Serialization.StreamingContext context) : base(info, context) { } 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/StringLiteralExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | 20 | namespace VBF.Compilers.Scanners 21 | { 22 | public sealed class StringLiteralExpression : RegularExpression 23 | { 24 | public StringLiteralExpression(string literal) 25 | : base(RegularExpressionType.StringLiteral) 26 | { 27 | Literal = literal; 28 | } 29 | 30 | public new string Literal { get; private set; } 31 | 32 | public override string ToString() 33 | { 34 | return Literal; 35 | } 36 | 37 | internal override Func>[] GetCompactableCharSets() 38 | { 39 | return new Func>[0]; 40 | } 41 | 42 | internal override HashSet GetUncompactableCharSet() 43 | { 44 | return new HashSet(Literal); 45 | } 46 | 47 | internal override T Accept(RegularExpressionConverter converter) 48 | { 49 | return converter.ConvertStringLiteral(this); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/SymbolExpression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Globalization; 20 | 21 | namespace VBF.Compilers.Scanners 22 | { 23 | /// 24 | /// Represents a regular expression accepts a literal character 25 | /// 26 | public class SymbolExpression : RegularExpression 27 | { 28 | public SymbolExpression(char symbol) 29 | : base(RegularExpressionType.Symbol) 30 | { 31 | Symbol = symbol; 32 | } 33 | 34 | public new char Symbol { get; private set; } 35 | 36 | public override string ToString() 37 | { 38 | return Symbol.ToString(CultureInfo.InvariantCulture); 39 | } 40 | 41 | internal override Func>[] GetCompactableCharSets() 42 | { 43 | return new Func>[0]; 44 | } 45 | 46 | internal override HashSet GetUncompactableCharSet() 47 | { 48 | var result = new HashSet {Symbol}; 49 | 50 | return result; 51 | } 52 | 53 | internal override T Accept(RegularExpressionConverter converter) 54 | { 55 | return converter.ConvertSymbol(this); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/Token.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | 19 | namespace VBF.Compilers.Scanners 20 | { 21 | public class Token : IEquatable 22 | { 23 | public Token(int index, string description, int lexerIndex) 24 | { 25 | Index = index; 26 | Description = description; 27 | LexerIndex = lexerIndex; 28 | } 29 | 30 | public int Index { get; private set; } 31 | public int LexerIndex { get; private set; } 32 | public string Description { get; private set; } 33 | 34 | public bool Equals(Token other) 35 | { 36 | if (other == null) 37 | { 38 | return false; 39 | } 40 | 41 | return Index == other.Index; 42 | } 43 | 44 | public override int GetHashCode() 45 | { 46 | return Index; 47 | } 48 | 49 | public override bool Equals(object obj) 50 | { 51 | return Equals(obj as Token); 52 | } 53 | 54 | public override string ToString() 55 | { 56 | return Description; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/TokenInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Diagnostics; 18 | using VBF.Compilers.Scanners.Generator; 19 | 20 | namespace VBF.Compilers.Scanners 21 | { 22 | [DebuggerDisplay("Index: {Tag.Index} {Tag.ToString()}")] 23 | public class TokenInfo 24 | { 25 | internal TokenInfo(RegularExpression definition, Lexicon lexicon, Lexer state, Token tag) 26 | { 27 | Lexicon = lexicon; 28 | Definition = definition; 29 | State = state; 30 | 31 | Tag = tag; 32 | } 33 | 34 | public Token Tag { get; private set; } 35 | public Lexicon Lexicon { get; private set; } 36 | public Lexer State { get; private set; } 37 | public RegularExpression Definition { get; private set; } 38 | 39 | public NFAModel CreateFiniteAutomatonModel(NFAConverter converter) 40 | { 41 | NFAModel nfa = converter.Convert(Definition); 42 | 43 | Debug.Assert(nfa.TailState != null); 44 | 45 | nfa.TailState.TokenIndex = Tag.Index; 46 | 47 | return nfa; 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.Scanners/UnitTestVisibility.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #define UnitTestEnabled 18 | using System.Runtime.CompilerServices; 19 | 20 | #if UnitTestEnabled 21 | [assembly: InternalsVisibleTo("Compilers.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100cf30f429fa1e11b6c616f6f9f03586f0ae3f307e347134854a48e1c57c54a5751bb9248ecd645430661e3ed06c9bc3b2c8083bbd1783310f39f6dcc475042225352a055001cc6652756c6daf1889c2d0cf26e74e63dd441f584cf6f5665cba5ca11837a4cbd5516367582fa5987efe5ab7cf69d1b75cfd36a3f745afddae90d1")] 22 | #endif -------------------------------------------------------------------------------- /src/Compilers/Compilers.UnitTests/Compilers.UnitTests.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ninputer/VBF/bd504d6a62a15a351a0e8ff3ce9a13de3914da78/src/Compilers/Compilers.UnitTests/Compilers.UnitTests.snk -------------------------------------------------------------------------------- /src/Compilers/Compilers.UnitTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("Compilers.UnitTests")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Ninputer")] 27 | [assembly: AssemblyProduct("VBF.Compilers")] 28 | [assembly: AssemblyCopyright("Copyright © 2012 Fan Shi")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("c460830f-b13e-4543-b32f-7a2b805933de")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | [assembly: AssemblyVersion("1.0.0.0")] 50 | [assembly: AssemblyFileVersion("1.0.0.0")] 51 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.UnitTests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /src/Compilers/Compilers.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | DFA 3 | NFA 4 | NT -------------------------------------------------------------------------------- /src/Compilers/Key/PublicKeyAndToken.txt: -------------------------------------------------------------------------------- 1 | 2 | Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.18020 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Public key (hash algorithm: sha1): 6 | 0024000004800000940000000602000000240000525341310004000001000100e1ed7107c6d438 7 | 4602ebd9d4c505f90b8540a4fbf049b4d6bca639dd664805c4a430fc7a09faf490461524afa5df 8 | 9fea2a299545aed52d38ef254fe81bf03684faef8474d8bd3b837576f69d26da2014d4e52472ac 9 | a1b45e24260e1bda57b80be241ed55eade92bc1791a0e10d1f300aedc5b4dbadfdc6e869a09e45 10 | 36467fc4 11 | 12 | Public key token is f24334f525609297 13 | -------------------------------------------------------------------------------- /src/Compilers/Key/VBF.Public.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ninputer/VBF/bd504d6a62a15a351a0e8ff3ce9a13de3914da78/src/Compilers/Key/VBF.Public.snk -------------------------------------------------------------------------------- /src/Samples/MiniSharp/ArrayType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp 18 | { 19 | public class ArrayType : TypeBase 20 | { 21 | public static readonly ArrayType IntArray = new ArrayType() { Name = "int[]", ElementType = PrimaryType.Int }; 22 | public static readonly ArrayType StrArray = new ArrayType() { Name = "string[]", ElementType = PrimaryType.String }; 23 | public TypeBase ElementType { get; set; } 24 | 25 | public override bool IsAssignableFrom(TypeBase type) 26 | { 27 | CodeClassType elementClassType = ElementType as CodeClassType; 28 | ArrayType arrayType = type as ArrayType; 29 | 30 | if (elementClassType != null && arrayType != null) 31 | { 32 | return elementClassType.IsAssignableFrom(arrayType.ElementType); 33 | } 34 | 35 | return false; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/ArrayAssign.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class ArrayAssign : Statement 22 | { 23 | public ArrayAssign(LexemeValue arrayName, Expression index, Expression value) 24 | { 25 | Array = new VariableRef(arrayName); 26 | Index = index; 27 | Value = value; 28 | } 29 | 30 | public VariableRef Array{ get; private set; } 31 | public Expression Index { get; private set; } 32 | public Expression Value { get; private set; } 33 | 34 | public override T Accept(IAstVisitor visitor) 35 | { 36 | return visitor.VisitArrayAssign(this); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/ArrayLength.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class ArrayLength : Expression 22 | { 23 | public ArrayLength(Expression array, SourceSpan lengthSpan) 24 | { 25 | Array = array; 26 | LengthSpan = lengthSpan; 27 | } 28 | 29 | public Expression Array { get; private set; } 30 | public SourceSpan LengthSpan { get; private set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitArrayLength(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/ArrayLookup.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class ArrayLookup : Expression 22 | { 23 | public ArrayLookup(Expression array, Expression index, SourceSpan indexSpan) 24 | { 25 | Array = array; 26 | Index = index; 27 | IndexSpan = indexSpan; 28 | } 29 | 30 | public Expression Array { get; set; } 31 | public Expression Index { get; private set; } 32 | public SourceSpan IndexSpan { get; private set; } 33 | 34 | public override T Accept(IAstVisitor visitor) 35 | { 36 | return visitor.VisitArrayLookup(this); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Assign.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class Assign : Statement 22 | { 23 | public Assign(LexemeValue varName, Expression value) 24 | { 25 | Variable = new VariableRef(varName); 26 | Value = value; 27 | } 28 | 29 | public VariableRef Variable { get; private set; } 30 | public Expression Value { get; internal set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitAssign(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/AstNode.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public abstract class AstNode 20 | { 21 | public abstract T Accept(IAstVisitor visitor); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Block.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | 20 | namespace VBF.MiniSharp.Ast 21 | { 22 | public class Block : Statement 23 | { 24 | public Block(IList statements) 25 | { 26 | if (statements == null) 27 | { 28 | return; 29 | } 30 | Statements = new ReadOnlyCollection(statements); 31 | } 32 | 33 | public ReadOnlyCollection Statements { get; private set; } 34 | 35 | public override T Accept(IAstVisitor visitor) 36 | { 37 | return visitor.VisitBlock(this); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/BooleanLiteral.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using VBF.Compilers; 19 | using VBF.Compilers.Scanners; 20 | 21 | namespace VBF.MiniSharp.Ast 22 | { 23 | public class BooleanLiteral : Expression 24 | { 25 | private SourceSpan m_literalSpan; 26 | 27 | public BooleanLiteral(LexemeValue literal) 28 | { 29 | Value = literal.Content == null ? false : Boolean.Parse(literal.Content); 30 | m_literalSpan = literal.Span; 31 | } 32 | 33 | public bool Value { get; private set; } 34 | 35 | public override T Accept(IAstVisitor visitor) 36 | { 37 | return visitor.VisitBooleanLiteral(this); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/BooleanType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public class BooleanType : Type 20 | { 21 | public override T Accept(IAstVisitor visitor) 22 | { 23 | return visitor.VisitBooleanType(this); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Call.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | using VBF.Compilers.Scanners; 20 | 21 | namespace VBF.MiniSharp.Ast 22 | { 23 | public class Call : Expression 24 | { 25 | public Call(Expression target, LexemeValue methodName, IList argList) 26 | { 27 | Target = target; 28 | Method = new MethodRef(methodName); 29 | Arguments = new ReadOnlyCollection(argList); 30 | } 31 | 32 | public Expression Target { get; private set; } 33 | public MethodRef Method { get; private set; } 34 | public ReadOnlyCollection Arguments { get; private set; } 35 | 36 | public override T Accept(IAstVisitor visitor) 37 | { 38 | return visitor.VisitCall(this); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/ClassDecl.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | using VBF.Compilers.Scanners; 20 | 21 | namespace VBF.MiniSharp.Ast 22 | { 23 | public class ClassDecl : AstNode 24 | { 25 | public ClassDecl(LexemeValue name, LexemeValue baseClassName, IList fields, IList methods) 26 | { 27 | BaseClass = new TypeRef(baseClassName); 28 | Name = name; 29 | Fields = new ReadOnlyCollection(fields); 30 | Methods = new ReadOnlyCollection(methods); 31 | } 32 | 33 | public TypeRef BaseClass { get; private set; } 34 | public LexemeValue Name { get; private set; } 35 | public ReadOnlyCollection Fields { get; private set; } 36 | public ReadOnlyCollection Methods { get; private set; } 37 | 38 | public TypeBase Type { get; set; } 39 | 40 | public override T Accept(IAstVisitor visitor) 41 | { 42 | return visitor.VisitClassDecl(this); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Expression.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public abstract class Expression : AstNode 20 | { 21 | public TypeBase ExpressionType { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/FieldDecl.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class FieldDecl : AstNode 22 | { 23 | public FieldDecl(Type type, LexemeValue fieldName) 24 | { 25 | Type = type; 26 | FieldName = fieldName; 27 | } 28 | 29 | public Field FieldInfo { get; set; } 30 | public Type Type { get; private set; } 31 | public LexemeValue FieldName { get; private set; } 32 | 33 | public override T Accept(IAstVisitor visitor) 34 | { 35 | return visitor.VisitFieldDecl(this); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Formal.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class Formal : AstNode 22 | { 23 | public Formal(Type type, LexemeValue paramName) 24 | { 25 | Type = type; 26 | ParameterName = paramName; 27 | } 28 | 29 | public Type Type { get; private set; } 30 | public LexemeValue ParameterName { get; private set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitFormal(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/IAstVisitor.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public interface IAstVisitor 20 | { 21 | T VisitArrayAssign(ArrayAssign ast); 22 | T VisitArrayLength(ArrayLength ast); 23 | T VisitArrayLookup(ArrayLookup ast); 24 | T VisitAssign(Assign ast); 25 | T VisitBinary(Binary ast); 26 | T VisitBlock(Block ast); 27 | T VisitBooleanLiteral(BooleanLiteral ast); 28 | T VisitBooleanType(BooleanType ast); 29 | T VisitCall(Call ast); 30 | T VisitClassDecl(ClassDecl ast); 31 | T VisitFieldDecl(FieldDecl ast); 32 | T VisitFormal(Formal ast); 33 | T VisitIdentifierType(IdentifierType ast); 34 | T VisitIfElse(IfElse ast); 35 | T VisitIntArrayType(IntArrayType ast); 36 | T VisitIntegerLiteral(IntegerLiteral ast); 37 | T VisitIntegerType(IntegerType ast); 38 | T VisitMainClass(MainClass ast); 39 | T VisitMethodDecl(MethodDecl ast); 40 | T VisitNewArray(NewArray ast); 41 | T VisitNewObject(NewObject ast); 42 | T VisitNot(Not ast); 43 | T VisitProgram(Program ast); 44 | T VisitThis(This ast); 45 | T VisitTypeConvert(TypeConvert ast); 46 | T VisitVarDecl(VarDecl ast); 47 | T VisitVariable(Variable ast); 48 | T VisitWhile(While ast); 49 | T VisitWriteLine(WriteLine ast); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/IdentifierType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class IdentifierType : Type 22 | { 23 | public IdentifierType(LexemeValue typeName) 24 | { 25 | Type = new TypeRef(typeName); 26 | } 27 | 28 | public TypeRef Type { get; private set; } 29 | 30 | public override T Accept(IAstVisitor visitor) 31 | { 32 | return visitor.VisitIdentifierType(this); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/IfElse.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class IfElse : Statement 22 | { 23 | public IfElse(Expression cond, Statement truePart, Statement falsePart, SourceSpan ifSpan, SourceSpan elseSpan) 24 | { 25 | Condition = cond; 26 | TruePart = truePart; 27 | FalsePart = falsePart; 28 | IfSpan = ifSpan; 29 | ElseSpan = elseSpan; 30 | } 31 | 32 | public Expression Condition { get; private set; } 33 | public Statement TruePart { get; private set; } 34 | public Statement FalsePart { get; private set; } 35 | public SourceSpan IfSpan { get; private set; } 36 | public SourceSpan ElseSpan { get; private set; } 37 | 38 | public override T Accept(IAstVisitor visitor) 39 | { 40 | return visitor.VisitIfElse(this); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/IntArrayType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public class IntArrayType : Type 20 | { 21 | 22 | public override T Accept(IAstVisitor visitor) 23 | { 24 | return visitor.VisitIntArrayType(this); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/IntegerLiteral.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class IntegerLiteral : Expression 22 | { 23 | public IntegerLiteral(LexemeValue literal) 24 | { 25 | Literal = literal; 26 | } 27 | 28 | public LexemeValue Literal { get; private set; } 29 | public int Value { get; set; } 30 | 31 | public override T Accept(IAstVisitor visitor) 32 | { 33 | return visitor.VisitIntegerLiteral(this); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/IntegerType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public class IntegerType : Type 20 | { 21 | public override T Accept(IAstVisitor visitor) 22 | { 23 | return visitor.VisitIntegerType(this); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/MainClass.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | using VBF.Compilers.Scanners; 20 | 21 | namespace VBF.MiniSharp.Ast 22 | { 23 | public class MainClass : AstNode 24 | { 25 | public MainClass(LexemeValue name, LexemeValue argName, IList statements) 26 | { 27 | Name = name; 28 | ArgName = argName; 29 | Statements = new ReadOnlyCollection(statements); 30 | } 31 | 32 | public LexemeValue Name { get; private set; } 33 | public LexemeValue ArgName { get; private set; } 34 | public ReadOnlyCollection Statements { get; private set; } 35 | 36 | public TypeBase Type { get; set; } 37 | 38 | public override T Accept(IAstVisitor visitor) 39 | { 40 | return visitor.VisitMainClass(this); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/MethodDecl.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | using VBF.Compilers.Scanners; 20 | 21 | namespace VBF.MiniSharp.Ast 22 | { 23 | public class MethodDecl : AstNode 24 | { 25 | public MethodDecl(LexemeValue name, Type retType, IList parameters, IList stmts, Expression retExp) 26 | { 27 | Name = name; 28 | ReturnType = retType; 29 | Parameters = new ReadOnlyCollection(parameters); 30 | Statements = stmts == null ? null : new ReadOnlyCollection(stmts); 31 | ReturnExpression = retExp; 32 | } 33 | 34 | public Method MethodInfo { get; set; } 35 | public Type ReturnType { get; private set; } 36 | public LexemeValue Name { get; private set; } 37 | public ReadOnlyCollection Parameters { get; private set; } 38 | public ReadOnlyCollection Statements { get; private set; } 39 | public Expression ReturnExpression { get; private set; } 40 | 41 | public override T Accept(IAstVisitor visitor) 42 | { 43 | return visitor.VisitMethodDecl(this); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/MethodRef.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class MethodRef 22 | { 23 | public MethodRef(LexemeValue name) 24 | { 25 | MethodName = name; 26 | } 27 | 28 | public LexemeValue MethodName { get; set; } 29 | public Method MethodInfo { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/NewArray.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class NewArray : Expression 22 | { 23 | public NewArray(Expression length, SourceSpan lengthSpan) 24 | { 25 | Length = length; 26 | LengthSpan = lengthSpan; 27 | } 28 | 29 | public Expression Length { get; private set; } 30 | public SourceSpan LengthSpan { get; private set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitNewArray(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/NewObject.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class NewObject : Expression 22 | { 23 | public NewObject(LexemeValue typeName) 24 | { 25 | Type = new TypeRef(typeName); 26 | } 27 | 28 | public TypeRef Type { get; private set; } 29 | 30 | public override T Accept(IAstVisitor visitor) 31 | { 32 | return visitor.VisitNewObject(this); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Not.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class Not : Expression 22 | { 23 | public Not(Expression exp, SourceSpan opSpan) 24 | { 25 | Operand = exp; 26 | OpSpan = opSpan; 27 | } 28 | 29 | public Expression Operand { get; private set; } 30 | public SourceSpan OpSpan { get; private set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitNot(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.Generic; 18 | using System.Collections.ObjectModel; 19 | 20 | namespace VBF.MiniSharp.Ast 21 | { 22 | public class Program : AstNode 23 | { 24 | public Program(MainClass mainClass, IList classes) 25 | { 26 | MainClass = mainClass; 27 | Classes = new ReadOnlyCollection(classes); 28 | } 29 | 30 | public MainClass MainClass { get; private set; } 31 | public ReadOnlyCollection Classes { get; private set; } 32 | 33 | public override T Accept(IAstVisitor visitor) 34 | { 35 | return visitor.VisitProgram(this); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Statement.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public abstract class Statement : AstNode 20 | { 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/This.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class This : Expression 22 | { 23 | public This(SourceSpan thisSpan) 24 | { 25 | ThisSpan = thisSpan; 26 | } 27 | 28 | public SourceSpan ThisSpan { get; private set; } 29 | 30 | public override T Accept(IAstVisitor visitor) 31 | { 32 | return visitor.VisitThis(this); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Type.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public abstract class Type : AstNode 20 | { 21 | public TypeBase ResolvedType { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/TypeConvert.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp.Ast 18 | { 19 | public class TypeConvert : Expression 20 | { 21 | public TypeConvert(Expression source, TypeBase targetType) 22 | { 23 | Source = source; 24 | ExpressionType = targetType; 25 | } 26 | 27 | public Expression Source { get; private set; } 28 | 29 | public override T Accept(IAstVisitor visitor) 30 | { 31 | return visitor.VisitTypeConvert(this); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/TypeRef.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class TypeRef 22 | { 23 | public TypeRef(LexemeValue name) 24 | { 25 | TypeName = name; 26 | } 27 | 28 | public LexemeValue TypeName { get; private set; } 29 | public TypeBase Type { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/VarDecl.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class VarDecl : Statement 22 | { 23 | public VarDecl(Type type, LexemeValue variableName) 24 | { 25 | Type = type; 26 | VariableName = variableName; 27 | } 28 | 29 | public Type Type { get; private set; } 30 | public LexemeValue VariableName { get; private set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitVarDecl(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/Variable.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class Variable : Expression 22 | { 23 | public Variable(LexemeValue name) 24 | { 25 | VariableRef = new VariableRef(name); 26 | } 27 | 28 | public VariableRef VariableRef { get; private set; } 29 | 30 | public override T Accept(IAstVisitor visitor) 31 | { 32 | return visitor.VisitVariable(this); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/VariableRef.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers.Scanners; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class VariableRef 22 | { 23 | public VariableRef(LexemeValue name) 24 | { 25 | VariableName = name; 26 | } 27 | 28 | public LexemeValue VariableName { get; private set; } 29 | public VariableInfo VariableInfo { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/While.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class While : Statement 22 | { 23 | public While(Expression cond, Statement body, SourceSpan whileSpan) 24 | { 25 | Condition = cond; 26 | LoopBody = body; 27 | WhileSpan = whileSpan; 28 | } 29 | 30 | public SourceSpan WhileSpan { get; private set; } 31 | public Expression Condition { get; private set; } 32 | public Statement LoopBody { get; private set; } 33 | 34 | public override T Accept(IAstVisitor visitor) 35 | { 36 | return visitor.VisitWhile(this); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Ast/WriteLine.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using VBF.Compilers; 18 | 19 | namespace VBF.MiniSharp.Ast 20 | { 21 | public class WriteLine : Statement 22 | { 23 | public WriteLine(Expression value, SourceSpan writelineSpan) 24 | { 25 | Value = value; 26 | WriteLineSpan = writelineSpan; 27 | } 28 | 29 | public Expression Value { get; private set; } 30 | public SourceSpan WriteLineSpan { get; private set; } 31 | 32 | public override T Accept(IAstVisitor visitor) 33 | { 34 | return visitor.VisitWriteLine(this); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/CodeClassType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.ObjectModel; 18 | 19 | namespace VBF.MiniSharp 20 | { 21 | public class CodeClassType : TypeBase 22 | { 23 | public CodeClassType() 24 | { 25 | Methods = new Collection(); 26 | StaticMethods = new Collection(); 27 | Fields = new VariableCollection(); 28 | } 29 | 30 | public bool IsStatic { get; set; } 31 | public CodeClassType BaseType { get; set; } 32 | public Collection Methods { get; private set; } 33 | public Collection StaticMethods { get; private set; } 34 | public VariableCollection Fields { get; private set; } 35 | 36 | public override bool IsAssignableFrom(TypeBase type) 37 | { 38 | CodeClassType otherClassType = type as CodeClassType; 39 | 40 | if (otherClassType == null) 41 | { 42 | return false; 43 | } 44 | 45 | if (otherClassType == this) 46 | { 47 | return true; 48 | } 49 | else 50 | { 51 | return IsAssignableFrom(otherClassType.BaseType); 52 | } 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/ExtensionTable.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Runtime.CompilerServices; 20 | 21 | namespace VBF.MiniSharp 22 | { 23 | public class ExtensionTable 24 | { 25 | private Dictionary m_extensionDict; 26 | 27 | public ExtensionTable() 28 | { 29 | m_extensionDict = new Dictionary(new ReferenceComparer()); 30 | } 31 | 32 | public void Set(object obj, T value) 33 | { 34 | m_extensionDict[obj] = value; 35 | } 36 | 37 | public void Remove(object obj) 38 | { 39 | m_extensionDict.Remove(obj); 40 | } 41 | 42 | public T Get(object obj) 43 | { 44 | if (m_extensionDict.ContainsKey(obj)) 45 | { 46 | return m_extensionDict[obj]; 47 | } 48 | else 49 | { 50 | return default(T); 51 | } 52 | 53 | } 54 | 55 | private class ReferenceComparer : IEqualityComparer 56 | { 57 | public new bool Equals(object x, object y) 58 | { 59 | return ReferenceEquals(x, y); 60 | } 61 | 62 | public int GetHashCode(object obj) 63 | { 64 | return RuntimeHelpers.GetHashCode(obj); 65 | } 66 | 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Field.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp 18 | { 19 | public class Field : VariableInfo 20 | { 21 | public TypeBase DeclaringType { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Method.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.ObjectModel; 18 | using System.Text; 19 | 20 | namespace VBF.MiniSharp 21 | { 22 | public class Method 23 | { 24 | public Method() 25 | { 26 | Parameters = new Collection(); 27 | } 28 | 29 | public TypeBase DeclaringType { get; set; } 30 | public string Name { get; set; } 31 | public TypeBase ReturnType { get; set; } 32 | public Collection Parameters { get; private set; } 33 | public bool IsStatic { get; set; } 34 | 35 | public string GetSignatureString() 36 | { 37 | StringBuilder signatureBuilder = new StringBuilder(); 38 | signatureBuilder.Append(DeclaringType.Name) 39 | .Append('.') 40 | .Append(Name) 41 | .Append('('); 42 | 43 | for (int i = 0; i < Parameters.Count - 1; i++) 44 | { 45 | signatureBuilder.Append(Parameters[i].Type.Name) 46 | .Append(',') 47 | .Append(' '); 48 | } 49 | 50 | if (Parameters.Count > 0) 51 | { 52 | signatureBuilder.Append(Parameters[Parameters.Count - 1].Type.Name); 53 | } 54 | 55 | signatureBuilder.Append(')'); 56 | 57 | return signatureBuilder.ToString(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/MiniSharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSharp", "MiniSharp.csproj", "{B107D197-8755-430F-98C8-51CA4C628F09}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {B107D197-8755-430F-98C8-51CA4C628F09}.Debug|x86.ActiveCfg = Debug|x86 13 | {B107D197-8755-430F-98C8-51CA4C628F09}.Debug|x86.Build.0 = Debug|x86 14 | {B107D197-8755-430F-98C8-51CA4C628F09}.Release|x86.ActiveCfg = Release|x86 15 | {B107D197-8755-430F-98C8-51CA4C628F09}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Parameter.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp 18 | { 19 | public class Parameter : VariableInfo 20 | { 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/PrimaryType.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp 18 | { 19 | public class PrimaryType : TypeBase 20 | { 21 | public static readonly PrimaryType Int = new PrimaryType() { Name = "int" }; 22 | public static readonly PrimaryType Boolean = new PrimaryType() { Name = "bool" }; 23 | public static readonly PrimaryType String = new PrimaryType() { Name = "string" }; 24 | public static readonly PrimaryType Void = new PrimaryType() { Name = "void" }; 25 | 26 | public static readonly PrimaryType Unknown = new PrimaryType() { Name = null }; 27 | 28 | public override bool IsAssignableFrom(TypeBase type) 29 | { 30 | if (this == type) 31 | { 32 | return true; 33 | } 34 | else 35 | { 36 | return false; 37 | } 38 | 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Reflection; 18 | using System.Runtime.InteropServices; 19 | 20 | // General Information about an assembly is controlled through the following 21 | // set of attributes. Change these attribute values to modify the information 22 | // associated with an assembly. 23 | [assembly: AssemblyTitle("MiniSharp")] 24 | [assembly: AssemblyDescription("")] 25 | [assembly: AssemblyConfiguration("")] 26 | [assembly: AssemblyCompany("Microsoft")] 27 | [assembly: AssemblyProduct("MiniSharp")] 28 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")] 29 | [assembly: AssemblyTrademark("")] 30 | [assembly: AssemblyCulture("")] 31 | 32 | // Setting ComVisible to false makes the types in this assembly not visible 33 | // to COM components. If you need to access a type in this assembly from 34 | // COM, set the ComVisible attribute to true on that type. 35 | [assembly: ComVisible(false)] 36 | 37 | // The following GUID is for the ID of the typelib if this project is exposed to COM 38 | [assembly: Guid("01f62cc8-e052-467c-a6ce-7c1140648748")] 39 | 40 | // Version information for an assembly consists of the following four values: 41 | // 42 | // Major Version 43 | // Minor Version 44 | // Build Number 45 | // Revision 46 | // 47 | // You can specify all the values or you can default the Build and Revision Numbers 48 | // by using the '*' as shown below: 49 | // [assembly: AssemblyVersion("1.0.*")] 50 | [assembly: AssemblyVersion("1.0.0.0")] 51 | [assembly: AssemblyFileVersion("1.0.0.0")] 52 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/TypeBase.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp 18 | { 19 | public class TypeBase 20 | { 21 | public string Name { get; set; } 22 | 23 | public virtual bool IsAssignableFrom(TypeBase type) 24 | { 25 | return false; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/TypeCollection.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System.Collections.ObjectModel; 18 | 19 | namespace VBF.MiniSharp 20 | { 21 | public class TypeCollection : KeyedCollection 22 | { 23 | protected override string GetKeyForItem(TypeBase item) 24 | { 25 | return item.Name; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/VariableCollection.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Collections.ObjectModel; 20 | 21 | namespace VBF.MiniSharp 22 | { 23 | public class VariableCollection : KeyedCollection where T : VariableInfo 24 | { 25 | public int m_Levels; 26 | private Stack> m_levelStack; 27 | 28 | public VariableCollection() 29 | { 30 | m_Levels = 0; 31 | m_levelStack = new Stack>(); 32 | } 33 | 34 | protected override string GetKeyForItem(T item) 35 | { 36 | return item.Name; 37 | } 38 | 39 | public void PushLevel() 40 | { 41 | m_levelStack.Push(new HashSet()); 42 | m_Levels++; 43 | } 44 | 45 | public void PopLevel() 46 | { 47 | if (m_Levels == 0) 48 | { 49 | throw new InvalidOperationException(); 50 | } 51 | 52 | var keysInLevel = m_levelStack.Pop(); 53 | m_Levels--; 54 | 55 | foreach (var key in keysInLevel) 56 | { 57 | Remove(key); 58 | } 59 | } 60 | 61 | protected override void InsertItem(int index, T item) 62 | { 63 | base.InsertItem(index, item); 64 | 65 | if (m_Levels > 0) 66 | { 67 | var keysInLevel = m_levelStack.Peek(); 68 | keysInLevel.Add(GetKeyForItem(item)); 69 | } 70 | 71 | 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/VariableInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Fan Shi 2 | // 3 | // This file is part of the VBF project. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | namespace VBF.MiniSharp 18 | { 19 | public class VariableInfo 20 | { 21 | public TypeBase Type { get; set; } 22 | public string Name { get; set; } 23 | public int Index { get; set; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Samples/MiniSharp/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/temp/Parser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using VBF.Compilers.Scanners; 6 | using System.Diagnostics; 7 | 8 | namespace VBF.Compilers.Parsers.Combinators 9 | { 10 | public class Parser 11 | { 12 | public ParserFunc Rule { get; private set; } 13 | 14 | public Parser() 15 | { 16 | 17 | } 18 | 19 | public Parser(ParserFunc rule) 20 | { 21 | Rule = rule; 22 | } 23 | 24 | public void Assign(Parser assignedParser) where U : class, T 25 | { 26 | CodeContract.RequiresArgumentNotNull(assignedParser, "assignedParser"); 27 | 28 | Rule = assignedParser.Rule; 29 | } 30 | 31 | public void Assign(Parser assignedParser) 32 | { 33 | CodeContract.RequiresArgumentNotNull(assignedParser, "assignedParser"); 34 | 35 | Rule = assignedParser.Rule; 36 | } 37 | 38 | public static Parser operator |(Parser leftParser, Parser rightParser) 39 | { 40 | CodeContract.RequiresArgumentNotNull(leftParser, "leftParser"); 41 | CodeContract.RequiresArgumentNotNull(rightParser, "rightParser"); 42 | 43 | return leftParser.Union(rightParser); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/temp/ParserFunc.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using VBF.Compilers.Scanners; 6 | 7 | namespace VBF.Compilers.Parsers.Combinators 8 | { 9 | public delegate IResult ParserFunc(ForkableScanner scanner); 10 | 11 | public interface IResult 12 | { 13 | T Value { get; } 14 | } 15 | 16 | public class Result : IResult 17 | { 18 | public T Value { get; private set; } 19 | 20 | public Result(T value) 21 | { 22 | Value = value; 23 | } 24 | } 25 | } 26 | --------------------------------------------------------------------------------