├── .gitattributes ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── antlr4cpp.sln ├── antlr4cpp ├── ReadMe.txt ├── antlr │ ├── test │ │ ├── test_graph_nodes.cpp │ │ ├── test_graph_nodes.hpp │ │ ├── test_interval_set.cpp │ │ ├── test_interval_set.hpp │ │ ├── test_visitor_inheritance.cpp │ │ └── test_visitor_inheritance.hpp │ └── v4 │ │ └── runtime │ │ ├── atn │ │ ├── atn_deserialization_options.hpp │ │ ├── atn_state.cpp │ │ ├── atn_state.hpp │ │ ├── atn_type.hpp │ │ ├── conflict_information.cpp │ │ ├── conflict_information.hpp │ │ ├── lexer_action.cpp │ │ ├── lexer_action.hpp │ │ ├── lexer_action_executor.cpp │ │ ├── lexer_action_executor.hpp │ │ ├── prediction_context.cpp │ │ ├── prediction_context.hpp │ │ ├── prediction_context_cache.cpp │ │ ├── prediction_context_cache.hpp │ │ ├── prediction_context_cache.inl │ │ ├── semantic_context.cpp │ │ ├── semantic_context.hpp │ │ ├── transition.cpp │ │ └── transition.hpp │ │ ├── dfa │ │ └── accept_state_information.hpp │ │ ├── misc │ │ ├── interval_set.hpp │ │ ├── murmur_hash.hpp │ │ ├── param_type.hpp │ │ ├── ptr_equal_to.hpp │ │ ├── ptr_hash.hpp │ │ ├── to_string.hpp │ │ ├── unordered_ptr_map.hpp │ │ ├── unordered_ptr_set.hpp │ │ ├── uuid.hpp │ │ └── visitor.hpp │ │ ├── token.hpp │ │ └── tree │ │ ├── parse_tree.cpp │ │ ├── parse_tree.hpp │ │ ├── parse_tree_listener.hpp │ │ ├── parse_tree_visitor.hpp │ │ ├── parse_tree_visitor.inl │ │ ├── parse_tree_walker.cpp │ │ └── parse_tree_walker.hpp ├── antlr4cpp.cpp ├── antlr4cpp.vcxproj ├── antlr4cpp.vcxproj.filters ├── stdafx.cpp ├── stdafx.h └── targetver.h └── appveyor.yml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Source files 2 | *.c text 3 | *.cpp text 4 | *.h text 5 | *.hpp text 6 | *.inl text 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Temporary and per-user files created by Visual Studio 2 | *.opensdf 3 | *.sdf 4 | *.suo 5 | *.vcxproj.user 6 | ipch/ 7 | .vs/ 8 | 9 | # Build output 10 | Debug/ 11 | Release/ 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: cpp 3 | compiler: clang 4 | script: 5 | - echo Not yet implemented 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributor Guide 2 | 3 | ## Contributor Agreement 4 | 5 | Before we can consider community-contributed code for inclusion in the project, the ANTLR contributor agreement must be 6 | signed. Don't worry, it's just another pull request. :smile: For details of the agreement see 7 | [contributors.txt](https://github.com/antlr/antlr4/blob/master/contributors.txt) in the **antlr/antlr4** repository. 8 | 9 | ## Open Discussion 10 | 11 | The most important part of an open source project is *open discussion*. If a developer, user, or other contributor has 12 | a question, comment, suggestion, or concern about any part of this project, **please speak up**: create an issue or add 13 | a comment to an existing issue or pull request. 14 | 15 | This policy especially affects the emoji described below. Any user or contributor may freely use these symbols to 16 | clarify intent during discussions. Questions and concerns from first-time contributors matter just as much as questions 17 | and concerns from the original project developers. 18 | 19 | ## License 20 | 21 | The code in this repository is licensed under the BSD 3-clause license. A complete description of the license, as well 22 | as license notices for 3rd-party code incorporated into this project, is available in the repository: 23 | 24 | * [LICENSE](https://github.com/antlr/antlr4-cpp/blob/master/LICENSE) 25 | 26 | ## Labels 27 | 28 | This project uses many labels for categorizing issues and pull requests. 29 | 30 | | Label | Meaning on Issue | Meaning on Pull Request | 31 | | --- | --- | --- | 32 | | bug | The issue concerns a bug in the code | The issue concerns a bug in the code | 33 | | enhancement | The issue is an improvement to the code | The pull request is an improvement to the code | 34 | | up for grabs | The issue is not assigned to a particular person. If you want to work on it, just add a comment saying so and it's yours! | Normally not applicable (see comments if observed on a pull request) | 35 | | question | The issue is a question | Normally not applicable (see comments if observed on a pull request) | 36 | | code review | n/a | The pull request is under review | 37 | | needs discussion | The issue needs further discussion before an actionable decision can be made | The pull request needs further discussion before an actionable decision can be made | 38 | | pull request | A pull request intended to address the issue has been created, but not yet merged | n/a | 39 | | blocked | The issue cannot be fixed until another issue, which may be external, is addressed | The pull request cannot be merged until another issue, which may be external, is addressed | 40 | | do not merge | n/a | The pull request should not be merged at this time. This could indicate a work-in-progress, a problem in the implementation code, or cases where the pull request depends on (is blocked by) another issue or pull request which has not been addressed. | 41 | | in progress | A developer is currently working on the issue | A developer is currently making updates to the code in the pull request | 42 | | fixed | The issue has been resolved | The pull request describes a new issue (i.e. no separate issue exists), and the content of the pull request was merged to fix the issue | 43 | | duplicate | Another issue or pull request contains the original report for this topic | Another pull request was submitted to correct the issue. This is generally only applied to pull requests after another pull request to correct the issue is merged. | 44 | | wontfix | The issue will not be corrected. The current behavior could be by design, out of scope, or cannot be changed due to the breaking changes policy for the project (see comments for details). | The pull request will not be merged due to a fundamental issue (see description for this label on issues) | 45 | 46 | ## Emoji 47 | 48 | GitHub provides several emoji which can be included in many areas of the site, especially including comments on issues 49 | and pull requests. To reduce confusion during evaluation of a pull request, the following emoji convey special intent. 50 | 51 | * :question: (`:question:`) Indicates a question. This emoji comes with **no strings attached**, which means it's fine 52 | to simply answer the question in another comment without making any changes to code. 53 | 54 | In general, pull requests with an unanswered question will not be merged until *someone* addresses it. This could be 55 | the original creator of the issue or pull request, another contributor to the project, or even the person who asked 56 | the question. 57 | 58 | * :exclamation: (`:exclamation:`) Indicates a problem with the code which will need to be addressed before changes can 59 | be merged into `master`. This *may* be accompanied by a specific suggestion for how to change the code. Any 60 | contributor is completely welcome to open a discussion regarding alternative solutions, even if the originally 61 | proposed change has already been made. 62 | 63 | Every active contributor to a project is likely to use the :exclamation: emoji (or similar) due to a simple 64 | misunderstanding or oversight in their evaluation. **This is absolutely fine.** If you notice a case like this, simply 65 | add a comment to clarify the intent and the exclamation can be considered resolved. 66 | 67 | Pull requests with an unresolved exclamation will not be merged until they are addressed by either a change in the 68 | code or a detailed explanation. While it is possible for code with an exclamation to be merged, this will usually only 69 | occur if *all* of the following conditions hold: 70 | 71 | * The rationale is fully and clearly explained 72 | * The code corrects an issue that is likely to affect users 73 | * All other proposed solutions are either too time-consuming to implement or are equally (or more) problematic 74 | 75 | * :bulb: (`:bulb:`) Indicates an idea or suggestion regarding a potential change in the code. These items will not 76 | necessarily block the inclusion of code into the `master` branch, especially in cases where suggestions are trivial 77 | and no other issues were found during code review. 78 | 79 | In certain trivial cases (e.g. a simple formatting change), a core contributor on the project may make the suggested 80 | change themselves in the process of merging a pull request. Like the :exclamation: emoji, :bulb: suggestions invite 81 | open discussion about alternative solutions from any contributor. 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | [The "BSD license"] 2 | Copyright (c) 2015 Terence Parr, Sam Harwell 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 3. The name of the author may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Experimental C++ 14 target for ANTLR 4 2 | 3 | [![Join the chat at https://gitter.im/antlr/antlr4-cpp](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/antlr/antlr4-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | This ANTLR 4 target is a very early work-in-progress, originally created for use in learning and practicing development 6 | with new features in C++ 11 and 14. The development process has not yet been determined, but we welcome input from the 7 | community at any time. 8 | 9 | ## Build status 10 | 11 | | OS | Compiler | Configuration | Status | 12 | | --- | --- | --- | --- | 13 | | Linux | Clang | None (disabled) | [![Build Status](https://travis-ci.org/antlr/antlr4-cpp.svg?branch=master)](https://travis-ci.org/antlr/antlr4-cpp) | 14 | | Windows | Visual Studio 2015 | Debug x86 | [![Build status](https://ci.appveyor.com/api/projects/status/evg0gcv76e8w50b3/branch/master?svg=true)](https://ci.appveyor.com/project/sharwell/antlr4-cpp/branch/master) | 15 | 16 | ## License 17 | 18 | This project is licensed under the 3-clause BSD license. See [LICENSE][] for details. 19 | 20 | [LICENSE]: https://github.com/antlr/antlr4-cpp/blob/master/LICENSE 21 | -------------------------------------------------------------------------------- /antlr4cpp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.22310.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "antlr4cpp", "antlr4cpp\antlr4cpp.vcxproj", "{2484DFD0-BDD6-48BC-9218-32D57DC7BA03}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {2484DFD0-BDD6-48BC-9218-32D57DC7BA03}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {2484DFD0-BDD6-48BC-9218-32D57DC7BA03}.Debug|Win32.Build.0 = Debug|Win32 16 | {2484DFD0-BDD6-48BC-9218-32D57DC7BA03}.Release|Win32.ActiveCfg = Release|Win32 17 | {2484DFD0-BDD6-48BC-9218-32D57DC7BA03}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /antlr4cpp/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | CONSOLE APPLICATION : antlr4cpp Project Overview 3 | ======================================================================== 4 | 5 | AppWizard has created this antlr4cpp application for you. 6 | 7 | This file contains a summary of what you will find in each of the files that 8 | make up your antlr4cpp application. 9 | 10 | 11 | antlr4cpp.vcxproj 12 | This is the main project file for VC++ projects generated using an Application Wizard. 13 | It contains information about the version of Visual C++ that generated the file, and 14 | information about the platforms, configurations, and project features selected with the 15 | Application Wizard. 16 | 17 | antlr4cpp.vcxproj.filters 18 | This is the filters file for VC++ projects generated using an Application Wizard. 19 | It contains information about the association between the files in your project 20 | and the filters. This association is used in the IDE to show grouping of files with 21 | similar extensions under a specific node (for e.g. ".cpp" files are associated with the 22 | "Source Files" filter). 23 | 24 | antlr4cpp.cpp 25 | This is the main application source file. 26 | 27 | ///////////////////////////////////////////////////////////////////////////// 28 | Other standard files: 29 | 30 | StdAfx.h, StdAfx.cpp 31 | These files are used to build a precompiled header (PCH) file 32 | named antlr4cpp.pch and a precompiled types file named StdAfx.obj. 33 | 34 | ///////////////////////////////////////////////////////////////////////////// 35 | Other notes: 36 | 37 | AppWizard uses "TODO:" comments to indicate parts of the source code you 38 | should add to or customize. 39 | 40 | ///////////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/test/test_graph_nodes.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 15 | #undef assert 16 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 17 | #endif 18 | 19 | namespace antlr { 20 | namespace test { 21 | 22 | using namespace antlr4::atn; 23 | 24 | namespace { 25 | 26 | // ------------ SUPPORT ------------------------- 27 | 28 | prediction_context_cache context_cache; 29 | 30 | std::shared_ptr a(bool fullContext); 31 | std::shared_ptr b(bool fullContext); 32 | std::shared_ptr c(bool fullContext); 33 | std::shared_ptr d(bool fullContext); 34 | std::shared_ptr u(bool fullContext); 35 | std::shared_ptr v(bool fullContext); 36 | std::shared_ptr w(bool fullContext); 37 | std::shared_ptr x(bool fullContext); 38 | std::shared_ptr y(bool fullContext); 39 | std::shared_ptr create_singleton(std::shared_ptr const& parent, int return_state); 40 | std::shared_ptr array(std::shared_ptr const& context); 41 | std::shared_ptr array(std::shared_ptr const& context0, std::shared_ptr const& context1); 42 | std::wstring to_dot_string(std::shared_ptr const& context); 43 | 44 | // ------------ TESTS ------------------------- 45 | 46 | void test_root_root() 47 | { 48 | std::shared_ptr r(context_cache.join(prediction_context::empty_local, prediction_context::empty_local)); 49 | std::wstring actual(to_dot_string(r)); 50 | std::wstring expecting = 51 | L"digraph G {\n" 52 | L"rankdir=LR;\n" 53 | L" s0[label=\"*\"];\n" 54 | L"}\n"; 55 | 56 | std::wcout << actual << std::endl; 57 | assert(actual == expecting); 58 | } 59 | 60 | void test_root_root_fullctx() 61 | { 62 | std::shared_ptr r(context_cache.join(prediction_context::empty_full, prediction_context::empty_full)); 63 | std::wstring actual(to_dot_string(r)); 64 | std::wstring expecting = 65 | L"digraph G {\n" 66 | L"rankdir=LR;\n" 67 | L" s0[label=\"$\"];\n" 68 | L"}\n"; 69 | 70 | std::wcout << actual << std::endl; 71 | assert(actual == expecting); 72 | } 73 | 74 | void test_x_root() 75 | { 76 | std::shared_ptr r(context_cache.join(x(false), prediction_context::empty_local)); 77 | std::wstring actual(to_dot_string(r)); 78 | std::wstring expecting = 79 | L"digraph G {\n" 80 | L"rankdir=LR;\n" 81 | L" s0[label=\"*\"];\n" 82 | L"}\n"; 83 | 84 | std::wcout << actual << std::endl; 85 | assert(actual == expecting); 86 | } 87 | 88 | void test_x_root_fullctx() 89 | { 90 | std::shared_ptr r(context_cache.join(x(true), prediction_context::empty_full)); 91 | std::wstring actual(to_dot_string(r)); 92 | std::wstring expecting = 93 | L"digraph G {\n" 94 | L"rankdir=LR;\n" 95 | L" s0[shape=record, label=\"|$\"];\n" 96 | L" s1[label=\"$\"];\n" 97 | L" s0:p0->s1[label=\"9\"];\n" 98 | L"}\n"; 99 | 100 | std::wcout << actual << std::endl; 101 | assert(actual == expecting); 102 | } 103 | 104 | void test_root_x() 105 | { 106 | std::shared_ptr r(context_cache.join(prediction_context::empty_local, x(false))); 107 | std::wstring actual(to_dot_string(r)); 108 | std::wstring expecting = 109 | L"digraph G {\n" 110 | L"rankdir=LR;\n" 111 | L" s0[label=\"*\"];\n" 112 | L"}\n"; 113 | 114 | std::wcout << actual << std::endl; 115 | assert(actual == expecting); 116 | } 117 | 118 | void test_root_x_fullctx() 119 | { 120 | std::shared_ptr r = context_cache.join(prediction_context::empty_full, x(true)); 121 | std::wstring actual(to_dot_string(r)); 122 | std::wstring expecting = 123 | L"digraph G {\n" 124 | L"rankdir=LR;\n" 125 | L" s0[shape=record, label=\"|$\"];\n" 126 | L" s1[label=\"$\"];\n" 127 | L" s0:p0->s1[label=\"9\"];\n" 128 | L"}\n"; 129 | 130 | std::wcout << actual << std::endl; 131 | assert(actual == expecting); 132 | } 133 | 134 | void test_a_a() 135 | { 136 | std::shared_ptr r = context_cache.join(a(false), a(false)); 137 | std::wstring actual(to_dot_string(r)); 138 | std::wstring expecting = 139 | L"digraph G {\n" 140 | L"rankdir=LR;\n" 141 | L" s0[label=\"0\"];\n" 142 | L" s1[label=\"*\"];\n" 143 | L" s0->s1[label=\"1\"];\n" 144 | L"}\n"; 145 | 146 | std::wcout << actual << std::endl; 147 | assert(actual == expecting); 148 | } 149 | 150 | void test_aroot_ax() 151 | { 152 | std::shared_ptr a1(a(false)); 153 | std::shared_ptr x(x(false)); 154 | std::shared_ptr a2(create_singleton(x, 1)); 155 | std::shared_ptr r(context_cache.join(a1, a2)); 156 | std::wstring actual(to_dot_string(r)); 157 | std::wstring expecting = 158 | L"digraph G {\n" 159 | L"rankdir=LR;\n" 160 | L" s0[label=\"0\"];\n" 161 | L" s1[label=\"*\"];\n" 162 | L" s0->s1[label=\"1\"];\n" 163 | L"}\n"; 164 | 165 | std::wcout << actual << std::endl; 166 | assert(actual == expecting); 167 | } 168 | 169 | void test_aroot_ax_fullctx() 170 | { 171 | std::shared_ptr a1(a(true)); 172 | std::shared_ptr x(x(true)); 173 | std::shared_ptr a2(create_singleton(x, 1)); 174 | std::shared_ptr r(context_cache.join(a1, a2)); 175 | std::wstring actual(to_dot_string(r)); 176 | std::wstring expecting = 177 | L"digraph G {\n" 178 | L"rankdir=LR;\n" 179 | L" s0[label=\"0\"];\n" 180 | L" s1[shape=record, label=\"|$\"];\n" 181 | L" s2[label=\"$\"];\n" 182 | L" s0->s1[label=\"1\"];\n" 183 | L" s1:p0->s2[label=\"9\"];\n" 184 | L"}\n"; 185 | 186 | std::wcout << actual << std::endl; 187 | assert(actual == expecting); 188 | } 189 | 190 | void test_axroot_aroot() 191 | { 192 | std::shared_ptr x(x(false)); 193 | std::shared_ptr a1(create_singleton(x, 1)); 194 | std::shared_ptr a2(a(false)); 195 | std::shared_ptr r(context_cache.join(a1, a2)); 196 | std::wstring actual(to_dot_string(r)); 197 | std::wstring expecting = 198 | L"digraph G {\n" 199 | L"rankdir=LR;\n" 200 | L" s0[label=\"0\"];\n" 201 | L" s1[label=\"*\"];\n" 202 | L" s0->s1[label=\"1\"];\n" 203 | L"}\n"; 204 | 205 | std::wcout << actual << std::endl; 206 | assert(actual == expecting); 207 | } 208 | 209 | void test_aaroot_aroot_root_fullCtx() 210 | { 211 | std::shared_ptr empty(prediction_context::empty_full); 212 | std::shared_ptr child1(create_singleton(empty, 8)); 213 | std::shared_ptr right(context_cache.join(empty, child1)); 214 | std::shared_ptr left(create_singleton(right, 8)); 215 | std::shared_ptr merged(context_cache.join(left, right)); 216 | std::wstring actual(to_dot_string(merged)); 217 | std::wstring expecting = 218 | L"digraph G {\n" 219 | L"rankdir=LR;\n" 220 | L" s0[shape=record, label=\"|$\"];\n" 221 | L" s1[shape=record, label=\"|$\"];\n" 222 | L" s2[label=\"$\"];\n" 223 | L" s0:p0->s1[label=\"8\"];\n" 224 | L" s1:p0->s2[label=\"8\"];\n" 225 | L"}\n"; 226 | 227 | std::wcout << actual << std::endl; 228 | assert(actual == expecting); 229 | } 230 | 231 | void test_axroot_aroot_fullctx() 232 | { 233 | std::shared_ptr x(x(true)); 234 | std::shared_ptr a1(create_singleton(x, 1)); 235 | std::shared_ptr a2(a(true)); 236 | std::shared_ptr r(context_cache.join(a1, a2)); 237 | std::wstring actual(to_dot_string(r)); 238 | std::wstring expecting = 239 | L"digraph G {\n" 240 | L"rankdir=LR;\n" 241 | L" s0[label=\"0\"];\n" 242 | L" s1[shape=record, label=\"|$\"];\n" 243 | L" s2[label=\"$\"];\n" 244 | L" s0->s1[label=\"1\"];\n" 245 | L" s1:p0->s2[label=\"9\"];\n" 246 | L"}\n"; 247 | 248 | std::wcout << actual << std::endl; 249 | assert(actual == expecting); 250 | } 251 | 252 | void test_a_b() 253 | { 254 | std::shared_ptr r(context_cache.join(a(false), b(false))); 255 | std::wstring actual(to_dot_string(r)); 256 | std::wstring expecting = 257 | L"digraph G {\n" 258 | L"rankdir=LR;\n" 259 | L" s0[shape=record, label=\"|\"];\n" 260 | L" s1[label=\"*\"];\n" 261 | L" s0:p0->s1[label=\"1\"];\n" 262 | L" s0:p1->s1[label=\"2\"];\n" 263 | L"}\n"; 264 | 265 | std::wcout << actual << std::endl; 266 | assert(actual == expecting); 267 | } 268 | 269 | void test_ax_ax_same() 270 | { 271 | std::shared_ptr x(x(false)); 272 | std::shared_ptr a1(create_singleton(x, 1)); 273 | std::shared_ptr a2(create_singleton(x, 1)); 274 | std::shared_ptr r(context_cache.join(a1, a2)); 275 | std::wstring actual(to_dot_string(r)); 276 | std::wstring expecting = 277 | L"digraph G {\n" 278 | L"rankdir=LR;\n" 279 | L" s0[label=\"0\"];\n" 280 | L" s1[label=\"1\"];\n" 281 | L" s2[label=\"*\"];\n" 282 | L" s0->s1[label=\"1\"];\n" 283 | L" s1->s2[label=\"9\"];\n" 284 | L"}\n"; 285 | 286 | std::wcout << actual << std::endl; 287 | assert(actual == expecting); 288 | } 289 | 290 | void test_ax_ax() 291 | { 292 | std::shared_ptr x1(x(false)); 293 | std::shared_ptr x2(x(false)); 294 | std::shared_ptr a1(create_singleton(x1, 1)); 295 | std::shared_ptr a2(create_singleton(x2, 1)); 296 | std::shared_ptr r(context_cache.join(a1, a2)); 297 | std::wstring actual(to_dot_string(r)); 298 | std::wstring expecting = 299 | L"digraph G {\n" 300 | L"rankdir=LR;\n" 301 | L" s0[label=\"0\"];\n" 302 | L" s1[label=\"1\"];\n" 303 | L" s2[label=\"*\"];\n" 304 | L" s0->s1[label=\"1\"];\n" 305 | L" s1->s2[label=\"9\"];\n" 306 | L"}\n"; 307 | 308 | std::wcout << actual << std::endl; 309 | assert(actual == expecting); 310 | } 311 | 312 | void test_abx_abx() 313 | { 314 | std::shared_ptr x1(x(false)); 315 | std::shared_ptr x2(x(false)); 316 | std::shared_ptr b1(create_singleton(x1, 2)); 317 | std::shared_ptr b2(create_singleton(x2, 2)); 318 | std::shared_ptr a1(create_singleton(b1, 1)); 319 | std::shared_ptr a2(create_singleton(b2, 1)); 320 | std::shared_ptr r(context_cache.join(a1, a2)); 321 | std::wstring actual(to_dot_string(r)); 322 | std::wstring expecting = 323 | L"digraph G {\n" 324 | L"rankdir=LR;\n" 325 | L" s0[label=\"0\"];\n" 326 | L" s1[label=\"1\"];\n" 327 | L" s2[label=\"2\"];\n" 328 | L" s3[label=\"*\"];\n" 329 | L" s0->s1[label=\"1\"];\n" 330 | L" s1->s2[label=\"2\"];\n" 331 | L" s2->s3[label=\"9\"];\n" 332 | L"}\n"; 333 | 334 | std::wcout << actual << std::endl; 335 | assert(actual == expecting); 336 | } 337 | 338 | void test_abx_acx() 339 | { 340 | std::shared_ptr x1(x(false)); 341 | std::shared_ptr x2(x(false)); 342 | std::shared_ptr b(create_singleton(x1, 2)); 343 | std::shared_ptr c(create_singleton(x2, 3)); 344 | std::shared_ptr a1(create_singleton(b, 1)); 345 | std::shared_ptr a2(create_singleton(c, 1)); 346 | std::shared_ptr r(context_cache.join(a1, a2)); 347 | std::wstring actual(to_dot_string(r)); 348 | std::wstring expecting = 349 | L"digraph G {\n" 350 | L"rankdir=LR;\n" 351 | L" s0[label=\"0\"];\n" 352 | L" s1[shape=record, label=\"|\"];\n" 353 | L" s2[label=\"2\"];\n" 354 | L" s3[label=\"*\"];\n" 355 | L" s0->s1[label=\"1\"];\n" 356 | L" s1:p0->s2[label=\"2\"];\n" 357 | L" s1:p1->s2[label=\"3\"];\n" 358 | L" s2->s3[label=\"9\"];\n" 359 | L"}\n"; 360 | 361 | std::wcout << actual << std::endl; 362 | assert(actual == expecting); 363 | } 364 | 365 | void test_ax_bx_same() 366 | { 367 | std::shared_ptr x(x(false)); 368 | std::shared_ptr a(create_singleton(x, 1)); 369 | std::shared_ptr b(create_singleton(x, 2)); 370 | std::shared_ptr r(context_cache.join(a, b)); 371 | std::wstring actual(to_dot_string(r)); 372 | std::wstring expecting = 373 | L"digraph G {\n" 374 | L"rankdir=LR;\n" 375 | L" s0[shape=record, label=\"|\"];\n" 376 | L" s1[label=\"1\"];\n" 377 | L" s2[label=\"*\"];\n" 378 | L" s0:p0->s1[label=\"1\"];\n" 379 | L" s0:p1->s1[label=\"2\"];\n" 380 | L" s1->s2[label=\"9\"];\n" 381 | L"}\n"; 382 | 383 | std::wcout << actual << std::endl; 384 | assert(actual == expecting); 385 | } 386 | 387 | void test_ax_bx() 388 | { 389 | std::shared_ptr x1(x(false)); 390 | std::shared_ptr x2(x(false)); 391 | std::shared_ptr a(create_singleton(x1, 1)); 392 | std::shared_ptr b(create_singleton(x2, 2)); 393 | std::shared_ptr r(context_cache.join(a, b)); 394 | std::wstring actual(to_dot_string(r)); 395 | std::wstring expecting = 396 | L"digraph G {\n" 397 | L"rankdir=LR;\n" 398 | L" s0[shape=record, label=\"|\"];\n" 399 | L" s1[label=\"1\"];\n" 400 | L" s2[label=\"*\"];\n" 401 | L" s0:p0->s1[label=\"1\"];\n" 402 | L" s0:p1->s1[label=\"2\"];\n" 403 | L" s1->s2[label=\"9\"];\n" 404 | L"}\n"; 405 | 406 | std::wcout << actual << std::endl; 407 | assert(actual == expecting); 408 | } 409 | 410 | void test_ax_by() 411 | { 412 | std::shared_ptr a(create_singleton(x(false), 1)); 413 | std::shared_ptr b(create_singleton(y(false), 2)); 414 | std::shared_ptr r(context_cache.join(a, b)); 415 | std::wstring actual(to_dot_string(r)); 416 | std::wstring expecting = 417 | L"digraph G {\n" 418 | L"rankdir=LR;\n" 419 | L" s0[shape=record, label=\"|\"];\n" 420 | L" s2[label=\"2\"];\n" 421 | L" s3[label=\"*\"];\n" 422 | L" s1[label=\"1\"];\n" 423 | L" s0:p0->s1[label=\"1\"];\n" 424 | L" s0:p1->s2[label=\"2\"];\n" 425 | L" s2->s3[label=\"10\"];\n" 426 | L" s1->s3[label=\"9\"];\n" 427 | L"}\n"; 428 | 429 | std::wcout << actual << std::endl; 430 | assert(actual == expecting); 431 | } 432 | 433 | void test_aroot_bx() 434 | { 435 | std::shared_ptr x2(x(false)); 436 | std::shared_ptr a(a(false)); 437 | std::shared_ptr b(create_singleton(x2, 2)); 438 | std::shared_ptr r(context_cache.join(a, b)); 439 | std::wstring actual(to_dot_string(r)); 440 | std::wstring expecting = 441 | L"digraph G {\n" 442 | L"rankdir=LR;\n" 443 | L" s0[shape=record, label=\"|\"];\n" 444 | L" s2[label=\"2\"];\n" 445 | L" s1[label=\"*\"];\n" 446 | L" s0:p0->s1[label=\"1\"];\n" 447 | L" s0:p1->s2[label=\"2\"];\n" 448 | L" s2->s1[label=\"9\"];\n" 449 | L"}\n"; 450 | 451 | std::wcout << actual << std::endl; 452 | assert(actual == expecting); 453 | } 454 | 455 | void test_aroot_bx_fullctx() 456 | { 457 | std::shared_ptr x2(x(true)); 458 | std::shared_ptr a(a(true)); 459 | std::shared_ptr b(create_singleton(x2, 2)); 460 | std::shared_ptr r(context_cache.join(a, b)); 461 | std::wstring actual(to_dot_string(r)); 462 | std::wstring expecting = 463 | L"digraph G {\n" 464 | L"rankdir=LR;\n" 465 | L" s0[shape=record, label=\"|\"];\n" 466 | L" s2[label=\"2\"];\n" 467 | L" s1[label=\"$\"];\n" 468 | L" s0:p0->s1[label=\"1\"];\n" 469 | L" s0:p1->s2[label=\"2\"];\n" 470 | L" s2->s1[label=\"9\"];\n" 471 | L"}\n"; 472 | 473 | std::wcout << actual << std::endl; 474 | assert(actual == expecting); 475 | } 476 | 477 | void test_aex_bfx() 478 | { 479 | std::shared_ptr x1(x(false)); 480 | std::shared_ptr x2(x(false)); 481 | std::shared_ptr e(create_singleton(x1, 5)); 482 | std::shared_ptr f(create_singleton(x2, 6)); 483 | std::shared_ptr a(create_singleton(e, 1)); 484 | std::shared_ptr b(create_singleton(f, 2)); 485 | std::shared_ptr r(context_cache.join(a, b)); 486 | std::wstring actual(to_dot_string(r)); 487 | std::wstring expecting = 488 | L"digraph G {\n" 489 | L"rankdir=LR;\n" 490 | L" s0[shape=record, label=\"|\"];\n" 491 | L" s2[label=\"2\"];\n" 492 | L" s3[label=\"3\"];\n" 493 | L" s4[label=\"*\"];\n" 494 | L" s1[label=\"1\"];\n" 495 | L" s0:p0->s1[label=\"1\"];\n" 496 | L" s0:p1->s2[label=\"2\"];\n" 497 | L" s2->s3[label=\"6\"];\n" 498 | L" s3->s4[label=\"9\"];\n" 499 | L" s1->s3[label=\"5\"];\n" 500 | L"}\n"; 501 | 502 | std::wcout << actual << std::endl; 503 | assert(actual == expecting); 504 | } 505 | 506 | // Array merges 507 | 508 | void test_Aroot_Aroot_fullctx() 509 | { 510 | std::shared_ptr A1(array(prediction_context::empty_full)); 511 | std::shared_ptr A2(array(prediction_context::empty_full)); 512 | std::shared_ptr r(context_cache.join(A1, A2)); 513 | std::wstring actual(to_dot_string(r)); 514 | std::wstring expecting = 515 | L"digraph G {\n" 516 | L"rankdir=LR;\n" 517 | L" s0[label=\"$\"];\n" 518 | L"}\n"; 519 | 520 | std::wcout << actual << std::endl; 521 | assert(actual == expecting); 522 | } 523 | 524 | void test_Aab_Ac() 525 | { 526 | // a,b + c 527 | std::shared_ptr a(a(false)); 528 | std::shared_ptr b(b(false)); 529 | std::shared_ptr c(c(false)); 530 | std::shared_ptr A1(array(a, b)); 531 | std::shared_ptr A2(array(c)); 532 | std::shared_ptr r(context_cache.join(A1, A2)); 533 | std::wstring actual(to_dot_string(r)); 534 | std::wstring expecting = 535 | L"digraph G {\n" 536 | L"rankdir=LR;\n" 537 | L" s0[shape=record, label=\"||\"];\n" 538 | L" s1[label=\"*\"];\n" 539 | L" s0:p0->s1[label=\"1\"];\n" 540 | L" s0:p1->s1[label=\"2\"];\n" 541 | L" s0:p2->s1[label=\"3\"];\n" 542 | L"}\n"; 543 | 544 | std::wcout << actual << std::endl; 545 | assert(actual == expecting); 546 | } 547 | 548 | void test_Aa_Aa() 549 | { 550 | std::shared_ptr a1(a(false)); 551 | std::shared_ptr a2(a(false)); 552 | std::shared_ptr A1(array(a1)); 553 | std::shared_ptr A2(array(a2)); 554 | std::shared_ptr r(context_cache.join(A1, A2)); 555 | std::wstring actual(to_dot_string(r)); 556 | std::wstring expecting = 557 | L"digraph G {\n" 558 | L"rankdir=LR;\n" 559 | L" s0[label=\"0\"];\n" 560 | L" s1[label=\"*\"];\n" 561 | L" s0->s1[label=\"1\"];\n" 562 | L"}\n"; 563 | 564 | std::wcout << actual << std::endl; 565 | assert(actual == expecting); 566 | } 567 | 568 | void test_Aa_Abc() 569 | { 570 | // a + b,c 571 | std::shared_ptr a(a(false)); 572 | std::shared_ptr b(b(false)); 573 | std::shared_ptr c(c(false)); 574 | std::shared_ptr A1(array(a)); 575 | std::shared_ptr A2(array(b, c)); 576 | std::shared_ptr r(context_cache.join(A1, A2)); 577 | std::wstring actual(to_dot_string(r)); 578 | std::wstring expecting = 579 | L"digraph G {\n" 580 | L"rankdir=LR;\n" 581 | L" s0[shape=record, label=\"||\"];\n" 582 | L" s1[label=\"*\"];\n" 583 | L" s0:p0->s1[label=\"1\"];\n" 584 | L" s0:p1->s1[label=\"2\"];\n" 585 | L" s0:p2->s1[label=\"3\"];\n" 586 | L"}\n"; 587 | 588 | std::wcout << actual << std::endl; 589 | assert(actual == expecting); 590 | } 591 | 592 | void test_Aac_Ab() 593 | { 594 | // a,c + b 595 | std::shared_ptr a(a(false)); 596 | std::shared_ptr b(b(false)); 597 | std::shared_ptr c(c(false)); 598 | std::shared_ptr A1(array(a, c)); 599 | std::shared_ptr A2(array(b)); 600 | std::shared_ptr r(context_cache.join(A1, A2)); 601 | std::wstring actual(to_dot_string(r)); 602 | std::wstring expecting = 603 | L"digraph G {\n" 604 | L"rankdir=LR;\n" 605 | L" s0[shape=record, label=\"||\"];\n" 606 | L" s1[label=\"*\"];\n" 607 | L" s0:p0->s1[label=\"1\"];\n" 608 | L" s0:p1->s1[label=\"2\"];\n" 609 | L" s0:p2->s1[label=\"3\"];\n" 610 | L"}\n"; 611 | 612 | std::wcout << actual << std::endl; 613 | assert(actual == expecting); 614 | } 615 | 616 | void test_Aab_Aa() 617 | { 618 | // a,b + a 619 | std::shared_ptr A1(array(a(false), b(false))); 620 | std::shared_ptr A2(array(a(false))); 621 | std::shared_ptr r(context_cache.join(A1, A2)); 622 | std::wstring actual(to_dot_string(r)); 623 | std::wstring expecting = 624 | L"digraph G {\n" 625 | L"rankdir=LR;\n" 626 | L" s0[shape=record, label=\"|\"];\n" 627 | L" s1[label=\"*\"];\n" 628 | L" s0:p0->s1[label=\"1\"];\n" 629 | L" s0:p1->s1[label=\"2\"];\n" 630 | L"}\n"; 631 | 632 | std::wcout << actual << std::endl; 633 | assert(actual == expecting); 634 | } 635 | 636 | void test_Aab_Ab() 637 | { 638 | // a,b + b 639 | std::shared_ptr A1(array(a(false), b(false))); 640 | std::shared_ptr A2(array(b(false))); 641 | std::shared_ptr r(context_cache.join(A1, A2)); 642 | std::wstring actual(to_dot_string(r)); 643 | std::wstring expecting = 644 | L"digraph G {\n" 645 | L"rankdir=LR;\n" 646 | L" s0[shape=record, label=\"|\"];\n" 647 | L" s1[label=\"*\"];\n" 648 | L" s0:p0->s1[label=\"1\"];\n" 649 | L" s0:p1->s1[label=\"2\"];\n" 650 | L"}\n"; 651 | 652 | std::wcout << actual << std::endl; 653 | assert(actual == expecting); 654 | } 655 | 656 | void test_Aax_Aby() 657 | { 658 | // ax + by but in arrays 659 | std::shared_ptr a(create_singleton(x(false), 1)); 660 | std::shared_ptr b(create_singleton(y(false), 2)); 661 | std::shared_ptr A1(array(a)); 662 | std::shared_ptr A2(array(b)); 663 | std::shared_ptr r(context_cache.join(A1, A2)); 664 | std::wstring actual(to_dot_string(r)); 665 | std::wstring expecting = 666 | L"digraph G {\n" 667 | L"rankdir=LR;\n" 668 | L" s0[shape=record, label=\"|\"];\n" 669 | L" s2[label=\"2\"];\n" 670 | L" s3[label=\"*\"];\n" 671 | L" s1[label=\"1\"];\n" 672 | L" s0:p0->s1[label=\"1\"];\n" 673 | L" s0:p1->s2[label=\"2\"];\n" 674 | L" s2->s3[label=\"10\"];\n" 675 | L" s1->s3[label=\"9\"];\n" 676 | L"}\n"; 677 | 678 | std::wcout << actual << std::endl; 679 | assert(actual == expecting); 680 | } 681 | 682 | void test_Aax_Aay() 683 | { 684 | // ax + ay -> merged singleton a, array parent 685 | std::shared_ptr a1(create_singleton(x(false), 1)); 686 | std::shared_ptr a2(create_singleton(y(false), 1)); 687 | std::shared_ptr A1(array(a1)); 688 | std::shared_ptr A2(array(a2)); 689 | std::shared_ptr r(context_cache.join(A1, A2)); 690 | std::wstring actual(to_dot_string(r)); 691 | std::wstring expecting = 692 | L"digraph G {\n" 693 | L"rankdir=LR;\n" 694 | L" s0[label=\"0\"];\n" 695 | L" s1[shape=record, label=\"|\"];\n" 696 | L" s2[label=\"*\"];\n" 697 | L" s0->s1[label=\"1\"];\n" 698 | L" s1:p0->s2[label=\"9\"];\n" 699 | L" s1:p1->s2[label=\"10\"];\n" 700 | L"}\n"; 701 | 702 | std::wcout << actual << std::endl; 703 | assert(actual == expecting); 704 | } 705 | 706 | void test_Aaxc_Aayd() 707 | { 708 | // ax,c + ay,d -> merged a, array parent 709 | std::shared_ptr a1(create_singleton(x(false), 1)); 710 | std::shared_ptr a2(create_singleton(y(false), 1)); 711 | std::shared_ptr A1(array(a1, c(false))); 712 | std::shared_ptr A2(array(a2, d(false))); 713 | std::shared_ptr r(context_cache.join(A1, A2)); 714 | std::wstring actual(to_dot_string(r)); 715 | std::wstring expecting = 716 | L"digraph G {\n" 717 | L"rankdir=LR;\n" 718 | L" s0[shape=record, label=\"||\"];\n" 719 | L" s2[label=\"*\"];\n" 720 | L" s1[shape=record, label=\"|\"];\n" 721 | L" s0:p0->s1[label=\"1\"];\n" 722 | L" s0:p1->s2[label=\"3\"];\n" 723 | L" s0:p2->s2[label=\"4\"];\n" 724 | L" s1:p0->s2[label=\"9\"];\n" 725 | L" s1:p1->s2[label=\"10\"];\n" 726 | L"}\n"; 727 | 728 | std::wcout << actual << std::endl; 729 | assert(actual == expecting); 730 | } 731 | 732 | void test_Aaubv_Acwdx() 733 | { 734 | // au,bv + cw,dx -> [a,b,c,d]->[u,v,w,x] 735 | std::shared_ptr a(create_singleton(u(false), 1)); 736 | std::shared_ptr b(create_singleton(v(false), 2)); 737 | std::shared_ptr c(create_singleton(w(false), 3)); 738 | std::shared_ptr d(create_singleton(x(false), 4)); 739 | std::shared_ptr A1(array(a, b)); 740 | std::shared_ptr A2(array(c, d)); 741 | std::shared_ptr r(context_cache.join(A1, A2)); 742 | std::wstring actual(to_dot_string(r)); 743 | std::wstring expecting = 744 | L"digraph G {\n" 745 | L"rankdir=LR;\n" 746 | L" s0[shape=record, label=\"|||\"];\n" 747 | L" s4[label=\"4\"];\n" 748 | L" s5[label=\"*\"];\n" 749 | L" s3[label=\"3\"];\n" 750 | L" s2[label=\"2\"];\n" 751 | L" s1[label=\"1\"];\n" 752 | L" s0:p0->s1[label=\"1\"];\n" 753 | L" s0:p1->s2[label=\"2\"];\n" 754 | L" s0:p2->s3[label=\"3\"];\n" 755 | L" s0:p3->s4[label=\"4\"];\n" 756 | L" s4->s5[label=\"9\"];\n" 757 | L" s3->s5[label=\"8\"];\n" 758 | L" s2->s5[label=\"7\"];\n" 759 | L" s1->s5[label=\"6\"];\n" 760 | L"}\n"; 761 | 762 | std::wcout << actual << std::endl; 763 | assert(actual == expecting); 764 | } 765 | 766 | void test_Aaubv_Abvdx() 767 | { 768 | // au,bv + bv,dx -> [a,b,d]->[u,v,x] 769 | std::shared_ptr a(create_singleton(u(false), 1)); 770 | std::shared_ptr b1(create_singleton(v(false), 2)); 771 | std::shared_ptr b2(create_singleton(v(false), 2)); 772 | std::shared_ptr d(create_singleton(x(false), 4)); 773 | std::shared_ptr A1(array(a, b1)); 774 | std::shared_ptr A2(array(b2, d)); 775 | std::shared_ptr r(context_cache.join(A1, A2)); 776 | std::wstring actual(to_dot_string(r)); 777 | std::wstring expecting = 778 | L"digraph G {\n" 779 | L"rankdir=LR;\n" 780 | L" s0[shape=record, label=\"||\"];\n" 781 | L" s3[label=\"3\"];\n" 782 | L" s4[label=\"*\"];\n" 783 | L" s2[label=\"2\"];\n" 784 | L" s1[label=\"1\"];\n" 785 | L" s0:p0->s1[label=\"1\"];\n" 786 | L" s0:p1->s2[label=\"2\"];\n" 787 | L" s0:p2->s3[label=\"4\"];\n" 788 | L" s3->s4[label=\"9\"];\n" 789 | L" s2->s4[label=\"7\"];\n" 790 | L" s1->s4[label=\"6\"];\n" 791 | L"}\n"; 792 | 793 | std::wcout << actual << std::endl; 794 | assert(actual == expecting); 795 | } 796 | 797 | void test_Aaubv_Abwdx() 798 | { 799 | // au,bv + bw,dx -> [a,b,d]->[u,[v,w],x] 800 | std::shared_ptr a(create_singleton(u(false), 1)); 801 | std::shared_ptr b1(create_singleton(v(false), 2)); 802 | std::shared_ptr b2(create_singleton(w(false), 2)); 803 | std::shared_ptr d(create_singleton(x(false), 4)); 804 | std::shared_ptr A1(array(a, b1)); 805 | std::shared_ptr A2(array(b2, d)); 806 | std::shared_ptr r(context_cache.join(A1, A2)); 807 | std::wstring actual(to_dot_string(r)); 808 | std::wstring expecting = 809 | L"digraph G {\n" 810 | L"rankdir=LR;\n" 811 | L" s0[shape=record, label=\"||\"];\n" 812 | L" s3[label=\"3\"];\n" 813 | L" s4[label=\"*\"];\n" 814 | L" s2[shape=record, label=\"|\"];\n" 815 | L" s1[label=\"1\"];\n" 816 | L" s0:p0->s1[label=\"1\"];\n" 817 | L" s0:p1->s2[label=\"2\"];\n" 818 | L" s0:p2->s3[label=\"4\"];\n" 819 | L" s3->s4[label=\"9\"];\n" 820 | L" s2:p0->s4[label=\"7\"];\n" 821 | L" s2:p1->s4[label=\"8\"];\n" 822 | L" s1->s4[label=\"6\"];\n" 823 | L"}\n"; 824 | 825 | std::wcout << actual << std::endl; 826 | assert(actual == expecting); 827 | } 828 | 829 | void test_Aaubv_Abvdu() 830 | { 831 | // au,bv + bv,du -> [a,b,d]->[u,v,u]; u,v shared 832 | std::shared_ptr a(create_singleton(u(false), 1)); 833 | std::shared_ptr b1(create_singleton(v(false), 2)); 834 | std::shared_ptr b2(create_singleton(v(false), 2)); 835 | std::shared_ptr d(create_singleton(u(false), 4)); 836 | std::shared_ptr A1(array(a, b1)); 837 | std::shared_ptr A2(array(b2, d)); 838 | std::shared_ptr r(context_cache.join(A1, A2)); 839 | std::wstring actual(to_dot_string(r)); 840 | std::wstring expecting = 841 | L"digraph G {\n" 842 | L"rankdir=LR;\n" 843 | L" s0[shape=record, label=\"||\"];\n" 844 | L" s2[label=\"2\"];\n" 845 | L" s3[label=\"*\"];\n" 846 | L" s1[label=\"1\"];\n" 847 | L" s0:p0->s1[label=\"1\"];\n" 848 | L" s0:p1->s2[label=\"2\"];\n" 849 | L" s0:p2->s1[label=\"4\"];\n" 850 | L" s2->s3[label=\"7\"];\n" 851 | L" s1->s3[label=\"6\"];\n" 852 | L"}\n"; 853 | 854 | std::wcout << actual << std::endl; 855 | assert(actual == expecting); 856 | } 857 | 858 | void test_Aaubu_Acudu() 859 | { 860 | // au,bu + cu,du -> [a,b,c,d]->[u,u,u,u] 861 | std::shared_ptr a(create_singleton(u(false), 1)); 862 | std::shared_ptr b(create_singleton(u(false), 2)); 863 | std::shared_ptr c(create_singleton(u(false), 3)); 864 | std::shared_ptr d(create_singleton(u(false), 4)); 865 | std::shared_ptr A1(array(a, b)); 866 | std::shared_ptr A2(array(c, d)); 867 | std::shared_ptr r(context_cache.join(A1, A2)); 868 | std::wstring actual(to_dot_string(r)); 869 | std::wstring expecting = 870 | L"digraph G {\n" 871 | L"rankdir=LR;\n" 872 | L" s0[shape=record, label=\"|||\"];\n" 873 | L" s1[label=\"1\"];\n" 874 | L" s2[label=\"*\"];\n" 875 | L" s0:p0->s1[label=\"1\"];\n" 876 | L" s0:p1->s1[label=\"2\"];\n" 877 | L" s0:p2->s1[label=\"3\"];\n" 878 | L" s0:p3->s1[label=\"4\"];\n" 879 | L" s1->s2[label=\"6\"];\n" 880 | L"}\n"; 881 | 882 | std::wcout << actual << std::endl; 883 | assert(actual == expecting); 884 | } 885 | 886 | // ------------ SUPPORT ------------------------- 887 | 888 | std::shared_ptr a(bool fullContext) { 889 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 1); 890 | } 891 | 892 | std::shared_ptr b(bool fullContext) { 893 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 2); 894 | } 895 | 896 | std::shared_ptr c(bool fullContext) { 897 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 3); 898 | } 899 | 900 | std::shared_ptr d(bool fullContext) { 901 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 4); 902 | } 903 | 904 | std::shared_ptr u(bool fullContext) { 905 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 6); 906 | } 907 | 908 | std::shared_ptr v(bool fullContext) { 909 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 7); 910 | } 911 | 912 | std::shared_ptr w(bool fullContext) { 913 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 8); 914 | } 915 | 916 | std::shared_ptr x(bool fullContext) { 917 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 9); 918 | } 919 | 920 | std::shared_ptr y(bool fullContext) { 921 | return create_singleton(fullContext ? prediction_context::empty_full : prediction_context::empty_local, 10); 922 | } 923 | 924 | std::shared_ptr create_singleton(std::shared_ptr const& parent, int return_state) { 925 | return context_cache.get_child(parent, return_state); 926 | } 927 | 928 | std::shared_ptr array(std::shared_ptr const& context) { 929 | return context; 930 | } 931 | 932 | std::shared_ptr array(std::shared_ptr const& context0, std::shared_ptr const& context1) { 933 | std::shared_ptr result = context0; 934 | result = context_cache.join(result, context1); 935 | return result; 936 | } 937 | 938 | // The default hash function for pointers is reference equality. Since C++ does not move objects in memory, we 939 | // don't need to provide the specialized hash mechanism here to avoid the computation of an identity hash code 940 | // like the Java code needs. 941 | typedef std::unordered_map, std::shared_ptr> identity_map; 942 | typedef std::unordered_map, size_t> id_map; 943 | 944 | std::wstring to_dot_string(std::shared_ptr const& context) 945 | { 946 | std::wostringstream nodes; 947 | std::wostringstream edges; 948 | 949 | identity_map visited; 950 | id_map context_ids; 951 | 952 | std::deque> work_list; 953 | visited.insert(std::make_pair(context, context)); 954 | context_ids.insert(std::make_pair(context, context_ids.size())); 955 | work_list.push_back(context); 956 | while (!work_list.empty()) 957 | { 958 | std::shared_ptr current(std::move(work_list.back())); 959 | work_list.pop_back(); 960 | nodes << L" s"; 961 | nodes << context_ids[current]; 962 | nodes << L"["; 963 | 964 | if (current->size() > 1) { 965 | nodes << L"shape=record, "; 966 | } 967 | 968 | nodes << L"label=\""; 969 | 970 | if (current->is_empty()) 971 | { 972 | nodes << (current->is_empty_local() ? L"*" : L"$"); 973 | } 974 | else if (current->size() > 1) 975 | { 976 | for (size_t i = 0; i < current->size(); i++) 977 | { 978 | if (i > 0) 979 | { 980 | nodes << L"|"; 981 | } 982 | 983 | nodes << L""; 984 | if (current->return_state(i) == prediction_context::empty_full_state_key) 985 | { 986 | nodes << L"$"; 987 | } 988 | else if (current->return_state(i) == prediction_context::empty_local_state_key) 989 | { 990 | nodes << L"*"; 991 | } 992 | } 993 | } 994 | else 995 | { 996 | nodes << context_ids[current]; 997 | } 998 | 999 | nodes << L"\"];\n"; 1000 | 1001 | for (size_t i = 0; i < current->size(); i++) 1002 | { 1003 | if (current->return_state(i) == prediction_context::empty_full_state_key 1004 | || current->return_state(i) == prediction_context::empty_local_state_key) 1005 | { 1006 | continue; 1007 | } 1008 | 1009 | if (visited.insert(std::make_pair(current->parent(i), current->parent(i))).second) 1010 | { 1011 | context_ids.insert(std::make_pair(current->parent(i), context_ids.size())); 1012 | work_list.push_back(current->parent(i)); 1013 | } 1014 | 1015 | edges << L" s" << context_ids[current]; 1016 | if (current->size() > 1) 1017 | { 1018 | edges << L":p" << i; 1019 | } 1020 | 1021 | edges << L"->"; 1022 | edges << L"s" << context_ids[current->parent(i)]; 1023 | edges << L"[label=\"" << current->return_state(i) << L"\"]"; 1024 | edges << L";\n"; 1025 | } 1026 | } 1027 | 1028 | std::wstring result; 1029 | result.append(L"digraph G {\n"); 1030 | result.append(L"rankdir=LR;\n"); 1031 | result.append(nodes.str()); 1032 | result.append(edges.str()); 1033 | result.append(L"}\n"); 1034 | return std::move(result); 1035 | } 1036 | } 1037 | 1038 | void test_graph_nodes() 1039 | { 1040 | test_root_root(); 1041 | test_root_root_fullctx(); 1042 | test_x_root(); 1043 | test_x_root_fullctx(); 1044 | test_root_x(); 1045 | test_root_x_fullctx(); 1046 | test_a_a(); 1047 | test_aroot_ax(); 1048 | test_aroot_ax_fullctx(); 1049 | test_axroot_aroot(); 1050 | test_aaroot_aroot_root_fullCtx(); 1051 | test_axroot_aroot_fullctx(); 1052 | test_a_b(); 1053 | test_ax_ax_same(); 1054 | test_ax_ax(); 1055 | test_abx_abx(); 1056 | test_abx_acx(); 1057 | test_ax_bx_same(); 1058 | test_ax_bx(); 1059 | test_ax_by(); 1060 | test_aroot_bx(); 1061 | test_aroot_bx_fullctx(); 1062 | test_aex_bfx(); 1063 | test_Aroot_Aroot_fullctx(); 1064 | test_Aab_Ac(); 1065 | test_Aa_Aa(); 1066 | test_Aa_Abc(); 1067 | test_Aac_Ab(); 1068 | test_Aab_Aa(); 1069 | test_Aab_Ab(); 1070 | test_Aax_Aby(); 1071 | test_Aax_Aay(); 1072 | test_Aaxc_Aayd(); 1073 | test_Aaubv_Acwdx(); 1074 | test_Aaubv_Abvdx(); 1075 | test_Aaubv_Abwdx(); 1076 | test_Aaubv_Abvdu(); 1077 | test_Aaubu_Acudu(); 1078 | } 1079 | 1080 | } 1081 | } 1082 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/test/test_graph_nodes.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr { 5 | namespace test { 6 | 7 | void test_graph_nodes(); 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/test/test_interval_set.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | #include "test_interval_set.hpp" 7 | 8 | #include 9 | #include 10 | 11 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 12 | #undef assert 13 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 14 | #endif 15 | 16 | namespace antlr { 17 | namespace test { 18 | 19 | using namespace antlr4; 20 | using namespace antlr4::misc; 21 | 22 | namespace { 23 | 24 | using interval_set = misc::interval_set; 25 | 26 | interval_set complete_char_set = interval_set::of(std::make_pair(0, 0xFFFE)); 27 | interval_set empty_set; 28 | 29 | std::wstring to_string(interval_set const& set) 30 | { 31 | return std::move(misc::to_string()(set)); 32 | } 33 | 34 | void test_single_element() 35 | { 36 | interval_set s = interval_set::of(99); 37 | std::wstring expecting = L"99"; 38 | std::wstring actual(to_string(s)); 39 | assert(actual == expecting); 40 | } 41 | 42 | void test_min() 43 | { 44 | assert(0 == complete_char_set.min()); 45 | assert(token::epsilon == interval_set::combine_or(complete_char_set, interval_set::of(token::epsilon)).min()); 46 | assert(token::eof == interval_set::combine_or(complete_char_set, interval_set::of(token::eof)).min()); 47 | } 48 | 49 | void test_isolated_elements() 50 | { 51 | interval_set s; 52 | s.insert(1); 53 | s.insert(L'z'); 54 | s.insert(L'\uFFF0'); 55 | std::wstring expecting = L"{1, 122, 65520}"; 56 | std::wstring actual = to_string(s); 57 | assert(actual == expecting); 58 | } 59 | 60 | void test_mixed_ranges_and_elements() 61 | { 62 | interval_set s; 63 | s.insert(1); 64 | s.insert(std::make_pair(L'a',L'z'+1)); 65 | s.insert(std::make_pair(L'0',L'9'+1)); 66 | std::wstring expecting = L"{1, 48..57, 97..122}"; 67 | std::wstring actual = to_string(s); 68 | assert(actual == expecting); 69 | } 70 | 71 | void test_simple_and() 72 | { 73 | interval_set s = interval_set::of(std::make_pair(10,20+1)); 74 | interval_set s2 = interval_set::of(std::make_pair(13,15+1)); 75 | std::wstring expecting = L"{13..15}"; 76 | std::wstring actual = to_string(interval_set::combine_and(s, s2)); 77 | assert(actual == expecting); 78 | } 79 | 80 | void test_range_and_isolated_element() 81 | { 82 | interval_set s = interval_set::of(std::make_pair('a','z'+1)); 83 | interval_set s2 = interval_set::of('d'); 84 | std::wstring expecting = L"100"; 85 | std::wstring actual = to_string(interval_set::combine_and(s, s2)); 86 | assert(actual == expecting); 87 | } 88 | 89 | void test_empty_intersection() 90 | { 91 | interval_set s = interval_set::of(std::make_pair('a','z'+1)); 92 | interval_set s2 = interval_set::of(std::make_pair('0','9'+1)); 93 | std::wstring expecting = L"{}"; 94 | std::wstring actual = to_string(interval_set::combine_and(s, s2)); 95 | assert(actual == expecting); 96 | } 97 | 98 | void test_empty_intersection_single_elements() 99 | { 100 | interval_set s = interval_set::of('a'); 101 | interval_set s2 = interval_set::of('d'); 102 | std::wstring expecting = L"{}"; 103 | std::wstring actual = to_string(interval_set::combine_and(s, s2)); 104 | assert(actual == expecting); 105 | } 106 | 107 | void test_not_single_element() 108 | { 109 | interval_set vocabulary = interval_set::of(std::make_pair(1,1000+1)); 110 | vocabulary.insert(std::make_pair(2000,3000 + 1)); 111 | interval_set s = interval_set::of(std::make_pair(50,50 + 1)); 112 | std::wstring expecting = L"{1..49, 51..1000, 2000..3000}"; 113 | std::wstring actual = to_string(interval_set::complement(s, vocabulary)); 114 | assert(actual == expecting); 115 | } 116 | 117 | void test_not_set() 118 | { 119 | interval_set vocabulary = interval_set::of(std::make_pair(1,1000 + 1)); 120 | interval_set s = interval_set::of(std::make_pair(50,60 + 1)); 121 | s.insert(5); 122 | s.insert(std::make_pair(250,300 + 1)); 123 | std::wstring expecting = L"{1..4, 6..49, 61..249, 301..1000}"; 124 | std::wstring actual = to_string(interval_set::complement(s, vocabulary)); 125 | assert(actual == expecting); 126 | } 127 | 128 | void test_not_equal_set() 129 | { 130 | interval_set vocabulary = interval_set::of(std::make_pair(1,1000 + 1)); 131 | interval_set s = interval_set::of(std::make_pair(1,1000 + 1)); 132 | std::wstring expecting = L"{}"; 133 | std::wstring actual = to_string(interval_set::complement(s, vocabulary)); 134 | assert(actual == expecting); 135 | } 136 | 137 | void test_not_set_edge_element() 138 | { 139 | interval_set vocabulary = interval_set::of(std::make_pair(1,2 + 1)); 140 | interval_set s = interval_set::of(1); 141 | std::wstring expecting = L"2"; 142 | std::wstring actual = to_string(interval_set::complement(s, vocabulary)); 143 | assert(actual == expecting); 144 | } 145 | 146 | void test_not_set_fragmented_vocabulary() 147 | { 148 | interval_set vocabulary = interval_set::of(std::make_pair(1,255 + 1)); 149 | vocabulary.insert(std::make_pair(1000,2000 + 1)); 150 | vocabulary.insert(9999); 151 | interval_set s = interval_set::of(std::make_pair(50, 60 + 1)); 152 | s.insert(3); 153 | s.insert(std::make_pair(250,300 + 1)); 154 | s.insert(10000); // this is outside range of vocab and should be ignored 155 | std::wstring expecting = L"{1..2, 4..49, 61..249, 1000..2000, 9999}"; 156 | std::wstring actual = to_string(interval_set::complement(s, vocabulary)); 157 | assert(actual == expecting); 158 | } 159 | 160 | void test_subtract_of_completely_contained_range() 161 | { 162 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 163 | interval_set s2 = interval_set::of(std::make_pair(12,15 + 1)); 164 | std::wstring expecting = L"{10..11, 16..20}"; 165 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 166 | assert(actual == expecting); 167 | } 168 | 169 | void test_subtract_from_set_with_eof() 170 | { 171 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 172 | s.insert(token::eof); 173 | interval_set s2 = interval_set::of(std::make_pair(12,15 + 1)); 174 | std::wstring expecting = L"{, 10..11, 16..20}"; 175 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 176 | assert(actual == expecting); 177 | } 178 | 179 | void test_subtract_of_overlapping_range_from_left() 180 | { 181 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 182 | interval_set s2 = interval_set::of(std::make_pair(5,11 + 1)); 183 | std::wstring expecting = L"{12..20}"; 184 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 185 | assert(actual == expecting); 186 | 187 | interval_set s3 = interval_set::of(std::make_pair(5,10 + 1)); 188 | expecting = L"{11..20}"; 189 | actual = to_string(interval_set::subtract(s, s3)); 190 | assert(actual == expecting); 191 | } 192 | 193 | void test_subtract_of_overlapping_range_from_right() 194 | { 195 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 196 | interval_set s2 = interval_set::of(std::make_pair(15,25 + 1)); 197 | std::wstring expecting = L"{10..14}"; 198 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 199 | assert(actual == expecting); 200 | 201 | interval_set s3 = interval_set::of(std::make_pair(20,25 + 1)); 202 | expecting = L"{10..19}"; 203 | actual = to_string(interval_set::subtract(s, s3)); 204 | assert(actual == expecting); 205 | } 206 | 207 | void test_subtract_of_completely_covered_range() 208 | { 209 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 210 | interval_set s2 = interval_set::of(std::make_pair(1,25 + 1)); 211 | std::wstring expecting = L"{}"; 212 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 213 | assert(actual == expecting); 214 | } 215 | 216 | void test_subtract_of_range_spanning_multiple_ranges() 217 | { 218 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 219 | s.insert(std::make_pair(30,40 + 1)); 220 | s.insert(std::make_pair(50,60 + 1)); // s has 3 ranges now: 10..20, 30..40, 50..60 221 | interval_set s2 = interval_set::of(std::make_pair(5,55 + 1)); // covers one and touches 2nd range 222 | std::wstring expecting = L"{56..60}"; 223 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 224 | assert(actual == expecting); 225 | 226 | interval_set s3 = interval_set::of(std::make_pair(15,55 + 1)); // touches both 227 | expecting = L"{10..14, 56..60}"; 228 | actual = to_string(interval_set::subtract(s, s3)); 229 | assert(actual == expecting); 230 | } 231 | 232 | /** The following was broken: 233 | {0..113, 115..65534}-{0..115, 117..65534}=116..65534 234 | */ 235 | void test_subtract_of_wacky_range() 236 | { 237 | interval_set s = interval_set::of(std::make_pair(0,113 + 1)); 238 | s.insert(std::make_pair(115,200 + 1)); 239 | interval_set s2 = interval_set::of(std::make_pair(0,115 + 1)); 240 | s2.insert(std::make_pair(117,200 + 1)); 241 | std::wstring expecting = L"116"; 242 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 243 | assert(actual == expecting); 244 | } 245 | 246 | void test_simple_equals() 247 | { 248 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 249 | interval_set s2 = interval_set::of(std::make_pair(10,20 + 1)); 250 | assert(s == s2); 251 | 252 | interval_set s3 = interval_set::of(std::make_pair(15,55 + 1)); 253 | assert(!(s == s3)); 254 | } 255 | 256 | void test_equals() 257 | { 258 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 259 | s.insert(2); 260 | s.insert(std::make_pair(499,501 + 1)); 261 | interval_set s2 = interval_set::of(std::make_pair(10,20 + 1)); 262 | s2.insert(2); 263 | s2.insert(std::make_pair(499,501 + 1)); 264 | assert(s == s2); 265 | 266 | interval_set s3 = interval_set::of(std::make_pair(10,20 + 1)); 267 | s3.insert(2); 268 | assert(!(s == s3)); 269 | } 270 | 271 | void test_single_element_minus_disjoint_set() 272 | { 273 | interval_set s = interval_set::of(std::make_pair(15,15 + 1)); 274 | interval_set s2 = interval_set::of(std::make_pair(1,5 + 1)); 275 | s2.insert(std::make_pair(10,20 + 1)); 276 | std::wstring expecting = L"{}"; // 15 - {1..5, 10..20} = {} 277 | std::wstring actual = to_string(interval_set::subtract(s, s2)); 278 | assert(actual == expecting); 279 | } 280 | 281 | void test_membership() 282 | { 283 | interval_set s = interval_set::of(std::make_pair(15,15 + 1)); 284 | s.insert(std::make_pair(50,60 + 1)); 285 | assert(!s.contains(0)); 286 | assert(!s.contains(20)); 287 | assert(!s.contains(100)); 288 | assert(s.contains(15)); 289 | assert(s.contains(55)); 290 | assert(s.contains(50)); 291 | assert(s.contains(60)); 292 | } 293 | 294 | // {2,15,18} & 10..20 295 | void test_intersection_with_two_contained_elements() 296 | { 297 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 298 | interval_set s2 = interval_set::of(std::make_pair(2,2 + 1)); 299 | s2.insert(15); 300 | s2.insert(18); 301 | std::wstring expecting = L"{15, 18}"; 302 | std::wstring actual = to_string(interval_set::combine_and(s, s2)); 303 | assert(actual == expecting); 304 | } 305 | 306 | void test_intersection_with_two_contained_elements_reversed() 307 | { 308 | interval_set s = interval_set::of(std::make_pair(10,20 + 1)); 309 | interval_set s2 = interval_set::of(std::make_pair(2,2 + 1)); 310 | s2.insert(15); 311 | s2.insert(18); 312 | std::wstring expecting = L"{15, 18}"; 313 | std::wstring actual = to_string(interval_set::combine_and(s2, s)); 314 | assert(actual == expecting); 315 | } 316 | 317 | void test_complement() 318 | { 319 | interval_set s = interval_set::of(std::make_pair(100,100 + 1)); 320 | s.insert(std::make_pair(101, 101 + 1)); 321 | interval_set s2 = interval_set::of(std::make_pair(100,102 + 1)); 322 | std::wstring expecting = L"102"; 323 | std::wstring actual = to_string(interval_set::complement(s, s2)); 324 | assert(actual == expecting); 325 | } 326 | 327 | void test_complement2() 328 | { 329 | interval_set s = interval_set::of(std::make_pair(100,101 + 1)); 330 | interval_set s2 = interval_set::of(std::make_pair(100,102 + 1)); 331 | std::wstring expecting = L"102"; 332 | std::wstring actual = to_string(interval_set::complement(s, s2)); 333 | assert(actual == expecting); 334 | } 335 | 336 | void test_complement3() 337 | { 338 | interval_set s = interval_set::of(std::make_pair(1,96 + 1)); 339 | s.insert(std::make_pair(99, L'\uFFFE' + 1)); 340 | std::wstring expecting = L"{97..98}"; 341 | std::wstring actual = to_string(interval_set::complement(s, std::make_pair(1, L'\uFFFE' + 1))); 342 | assert(actual == expecting); 343 | } 344 | 345 | void test_merge_of_ranges_and_single_values() 346 | { 347 | // {0..41, 42, 43..65534} 348 | interval_set s = interval_set::of(std::make_pair(0,41 + 1)); 349 | s.insert(42); 350 | s.insert(std::make_pair(43, 65534 + 1)); 351 | std::wstring expecting = L"{0..65534}"; 352 | std::wstring actual = to_string(s); 353 | assert(actual == expecting); 354 | } 355 | 356 | void test_merge_of_ranges_and_single_values_reverse() 357 | { 358 | interval_set s = interval_set::of(std::make_pair(43,65534 + 1)); 359 | s.insert(42); 360 | s.insert(std::make_pair(0, 41 + 1)); 361 | std::wstring expecting = L"{0..65534}"; 362 | std::wstring actual = to_string(s); 363 | assert(actual == expecting); 364 | } 365 | 366 | void test_merge_where_addition_merges_two_existing_intervals() 367 | { 368 | // 42, 10, {0..9, 11..41, 43..65534} 369 | interval_set s = interval_set::of(42); 370 | s.insert(10); 371 | s.insert(std::make_pair(0, 9 + 1)); 372 | s.insert(std::make_pair(43, 65534 + 1)); 373 | s.insert(std::make_pair(11, 41 + 1)); 374 | std::wstring expecting = L"{0..65534}"; 375 | std::wstring actual = to_string(s); 376 | assert(actual == expecting); 377 | } 378 | 379 | /** 380 | * This case is responsible for antlr/antlr4#153. 381 | * https://github.com/antlr/antlr4/issues/153 382 | */ 383 | void test_merge_where_addition_merges_three_existing_intervals() 384 | { 385 | interval_set s; 386 | s.insert(0); 387 | s.insert(3); 388 | s.insert(5); 389 | s.insert(std::make_pair(0, 7 + 1)); 390 | std::wstring expecting = L"{0..7}"; 391 | std::wstring actual = to_string(s); 392 | assert(actual == expecting); 393 | } 394 | 395 | void test_merge_with_double_overlap() 396 | { 397 | interval_set s = interval_set::of(std::make_pair(1,10 + 1)); 398 | s.insert(std::make_pair(20, 30 + 1)); 399 | s.insert(std::make_pair(5, 25 + 1)); // overlaps two! 400 | std::wstring expecting = L"{1..30}"; 401 | std::wstring actual = to_string(s); 402 | assert(actual == expecting); 403 | } 404 | 405 | void test_size() 406 | { 407 | interval_set s = interval_set::of(std::make_pair(20,30 + 1)); 408 | s.insert(std::make_pair(50, 55 + 1)); 409 | s.insert(std::make_pair(5, 19 + 1)); 410 | int32_t expecting = 32; 411 | int32_t actual = s.size(); 412 | assert(actual == expecting); 413 | } 414 | 415 | //void test_to_list() { 416 | // interval_set s = interval_set::of(std::make_pair(20, 25)); 417 | // s.insert(std::make_pair(50, 55)); 418 | // s.insert(std::make_pair(5, 5)); 419 | // std::wstring expecting = L"[5, 20, 21, 22, 23, 24, 25, 50, 51, 52, 53, 54, 55]"; 420 | // std::wstring actual = std::wstring.valueOf(s.toList()); 421 | // assert(actual == expecting); 422 | //} 423 | 424 | /** The following was broken: 425 | {'\u0000'..'s', 'u'..'\uFFFE'} & {'\u0000'..'q', 's'..'\uFFFE'}= 426 | {'\u0000'..'q', 's'}!!!! broken... 427 | 'q' is 113 ascii 428 | 'u' is 117 429 | */ 430 | void test_not_r_intersection_not_t() 431 | { 432 | interval_set s = interval_set::of(std::make_pair(0,'s' + 1)); 433 | s.insert(std::make_pair('u',200 + 1)); 434 | interval_set s2 = interval_set::of(std::make_pair(0,'q' + 1)); 435 | s2.insert(std::make_pair('s',200 + 1)); 436 | std::wstring expecting = L"{0..113, 115, 117..200}"; 437 | std::wstring actual = to_string(interval_set::combine_and(s, s2)); 438 | assert(actual == expecting); 439 | } 440 | 441 | void test_remove_single_element() 442 | { 443 | interval_set s = interval_set::of(std::make_pair(1,10 + 1)); 444 | s.insert(std::make_pair(-3,-3 + 1)); 445 | s.remove(-3); 446 | std::wstring expecting = L"{1..10}"; 447 | std::wstring actual = to_string(s); 448 | assert(actual == expecting); 449 | } 450 | 451 | void test_remove_left_side() 452 | { 453 | interval_set s = interval_set::of(std::make_pair(1,10 + 1)); 454 | s.insert(std::make_pair(-3,-3 + 1)); 455 | s.remove(1); 456 | std::wstring expecting = L"{-3, 2..10}"; 457 | std::wstring actual = to_string(s); 458 | assert(actual == expecting); 459 | } 460 | 461 | void test_remove_right_side() 462 | { 463 | interval_set s = interval_set::of(std::make_pair(1,10 + 1)); 464 | s.insert(std::make_pair(-3,-3 + 1)); 465 | s.remove(10); 466 | std::wstring expecting = L"{-3, 1..9}"; 467 | std::wstring actual = to_string(s); 468 | assert(actual == expecting); 469 | } 470 | 471 | void test_remove_middle_range() 472 | { 473 | interval_set s = interval_set::of(std::make_pair(1,10 + 1)); 474 | s.insert(std::make_pair(-3,-3 + 1)); 475 | s.remove(5); 476 | std::wstring expecting = L"{-3, 1..4, 6..10}"; 477 | std::wstring actual = to_string(s); 478 | assert(actual == expecting); 479 | } 480 | 481 | } 482 | 483 | void test_interval_set() 484 | { 485 | test_single_element(); 486 | test_min(); 487 | test_isolated_elements(); 488 | test_mixed_ranges_and_elements(); 489 | test_simple_and(); 490 | test_range_and_isolated_element(); 491 | test_empty_intersection(); 492 | test_empty_intersection_single_elements(); 493 | test_not_single_element(); 494 | test_not_set(); 495 | test_not_equal_set(); 496 | test_not_set_edge_element(); 497 | test_not_set_fragmented_vocabulary(); 498 | test_subtract_of_completely_contained_range(); 499 | test_subtract_from_set_with_eof(); 500 | test_subtract_of_overlapping_range_from_left(); 501 | test_subtract_of_overlapping_range_from_right(); 502 | test_subtract_of_completely_covered_range(); 503 | test_subtract_of_range_spanning_multiple_ranges(); 504 | test_subtract_of_wacky_range(); 505 | test_simple_equals(); 506 | test_equals(); 507 | test_single_element_minus_disjoint_set(); 508 | test_membership(); 509 | test_intersection_with_two_contained_elements(); 510 | test_intersection_with_two_contained_elements_reversed(); 511 | test_complement(); 512 | test_complement2(); 513 | test_complement3(); 514 | test_merge_of_ranges_and_single_values(); 515 | test_merge_of_ranges_and_single_values_reverse(); 516 | test_merge_where_addition_merges_two_existing_intervals(); 517 | test_merge_where_addition_merges_three_existing_intervals(); 518 | test_merge_with_double_overlap(); 519 | test_size(); 520 | //test_to_list(); 521 | test_not_r_intersection_not_t(); 522 | test_remove_single_element(); 523 | test_remove_left_side(); 524 | test_remove_right_side(); 525 | test_remove_middle_range(); 526 | } 527 | 528 | } 529 | } 530 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/test/test_interval_set.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr { 5 | namespace test { 6 | 7 | void test_interval_set(); 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/test/test_visitor_inheritance.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | #include "test_visitor_inheritance.hpp" 7 | 8 | #include 9 | #include 10 | 11 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 12 | #undef assert 13 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 14 | #endif 15 | 16 | namespace antlr { 17 | namespace test { 18 | 19 | using namespace antlr4; 20 | using namespace antlr4::misc; 21 | using namespace antlr4::tree; 22 | 23 | namespace { 24 | 25 | class sample_rule_node : public rule_node, public visitable 26 | { 27 | public: 28 | explicit sample_rule_node() 29 | : rule_node(nullptr) 30 | { 31 | } 32 | 33 | public: 34 | virtual std::shared_ptr parent() const override 35 | { 36 | throw std::runtime_error("not implemented"); 37 | } 38 | 39 | virtual std::shared_ptr child(size_t /*index*/) const override 40 | { 41 | throw std::runtime_error("not implemented"); 42 | } 43 | 44 | virtual size_t size() const override 45 | { 46 | return 0; 47 | //throw std::runtime_error("not implemented"); 48 | } 49 | 50 | virtual std::pair source_interval() const override 51 | { 52 | throw std::runtime_error("not implemented"); 53 | } 54 | 55 | virtual std::wstring text() const override 56 | { 57 | throw std::runtime_error("not implemented"); 58 | } 59 | 60 | virtual std::wstring tree_text() const override 61 | { 62 | throw std::runtime_error("not implemented"); 63 | } 64 | 65 | virtual void accept(parse_tree_visitor& visitor) const override 66 | { 67 | misc::visitor* typed_visitor = dynamic_cast*>(&visitor); 68 | if (typed_visitor) 69 | { 70 | typed_visitor->visit(*this); 71 | } 72 | else 73 | { 74 | visitor.visit_children(*this); 75 | } 76 | } 77 | 78 | virtual void accept(visitor& visitor) const override 79 | { 80 | visitor.visit(*this); 81 | } 82 | }; 83 | 84 | class sample_rule_node2 : public rule_node, public visitable 85 | { 86 | public: 87 | explicit sample_rule_node2() 88 | : rule_node(nullptr) 89 | { 90 | } 91 | 92 | public: 93 | virtual std::shared_ptr parent() const override 94 | { 95 | throw std::runtime_error("not implemented"); 96 | } 97 | 98 | virtual std::shared_ptr child(size_t /*index*/) const override 99 | { 100 | throw std::runtime_error("not implemented"); 101 | } 102 | 103 | virtual size_t size() const override 104 | { 105 | return 0; 106 | //throw std::runtime_error("not implemented"); 107 | } 108 | 109 | virtual std::pair source_interval() const override 110 | { 111 | throw std::runtime_error("not implemented"); 112 | } 113 | 114 | virtual std::wstring text() const override 115 | { 116 | throw std::runtime_error("not implemented"); 117 | } 118 | 119 | virtual std::wstring tree_text() const override 120 | { 121 | throw std::runtime_error("not implemented"); 122 | } 123 | 124 | virtual void accept(parse_tree_visitor& visitor) const override 125 | { 126 | misc::visitor* typed_visitor = dynamic_cast*>(&visitor); 127 | if (typed_visitor) 128 | { 129 | typed_visitor->visit(*this); 130 | } 131 | else 132 | { 133 | visitor.visit_children(*this); 134 | } 135 | } 136 | 137 | virtual void accept(visitor& visitor) const override 138 | { 139 | visitor.visit(*this); 140 | } 141 | }; 142 | 143 | class sample_parser_visitor : public visitor, public parse_tree_visitor 144 | { 145 | }; 146 | 147 | template 148 | class sample_parser_visitor_impl : public parse_tree_visitor_impl<_TResult>, public sample_parser_visitor 149 | { 150 | public: 151 | virtual void visit(parse_tree const& node) override 152 | { 153 | std::wcout << L"visiting unexpected call" << std::endl; 154 | parse_tree_visitor_impl<_TResult>::visit(node); 155 | }; 156 | 157 | virtual void visit(terminal_node const& node) override 158 | { 159 | parse_tree_visitor_impl<_TResult>::visit(node); 160 | }; 161 | 162 | virtual void visit(error_node const& node) override 163 | { 164 | parse_tree_visitor_impl<_TResult>::visit(node); 165 | }; 166 | 167 | virtual void visit_children(rule_node const& node) override 168 | { 169 | parse_tree_visitor_impl<_TResult>::visit_children(node); 170 | }; 171 | 172 | virtual void visit(sample_rule_node const& node) override 173 | { 174 | std::wcout << L"visiting sample rule node" << std::endl; 175 | visit_children(node); 176 | } 177 | 178 | virtual void visit(sample_rule_node2 const& node) override 179 | { 180 | std::wcout << L"visiting sample rule node 2" << std::endl; 181 | visit_children(node); 182 | } 183 | }; 184 | 185 | class general_parse_tree_visitor : public parse_tree_visitor_impl 186 | { 187 | public: 188 | virtual void visit(parse_tree const& node) override 189 | { 190 | std::wcout << L"visiting basic node" << std::endl; 191 | parse_tree_visitor_impl::visit(node); 192 | } 193 | }; 194 | 195 | } 196 | 197 | void test_visitor_inheritance() 198 | { 199 | sample_rule_node ptr; 200 | sample_rule_node2 ptr2; 201 | 202 | sample_parser_visitor_impl visitor1; 203 | visitor& typed_visitor = visitor1; 204 | visitor& typed_visitor1 = visitor1; 205 | visitor1.visit(ptr); 206 | visitor1.visit(ptr2); 207 | typed_visitor.visit(ptr); 208 | typed_visitor1.visit(ptr2); 209 | ptr.accept(typed_visitor); 210 | ptr2.accept(typed_visitor1); 211 | 212 | general_parse_tree_visitor visitor2; 213 | visitor2.visit(ptr); 214 | } 215 | 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/test/test_visitor_inheritance.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr { 5 | namespace test { 6 | 7 | void test_visitor_inheritance(); 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/atn_deserialization_options.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr4 { 5 | namespace atn { 6 | 7 | class atn_deserialization_options 8 | { 9 | private: 10 | bool _verify_atn; 11 | bool _generate_rule_bypass_transitions; 12 | bool _optimize; 13 | 14 | public: 15 | atn_deserialization_options() 16 | : _verify_atn(true) 17 | , _generate_rule_bypass_transitions(false) 18 | , _optimize(true) 19 | { 20 | } 21 | 22 | public: 23 | static atn_deserialization_options default_options() 24 | { 25 | return atn_deserialization_options(); 26 | } 27 | 28 | public: 29 | bool verify_atn() const 30 | { 31 | return _verify_atn; 32 | } 33 | 34 | void verify_atn(bool value) 35 | { 36 | _verify_atn = value; 37 | } 38 | 39 | bool generate_rule_bypass_transitions() const 40 | { 41 | return _generate_rule_bypass_transitions; 42 | } 43 | 44 | void generate_rule_bypass_transitions(bool value) 45 | { 46 | _generate_rule_bypass_transitions = value; 47 | } 48 | 49 | bool optimize() const 50 | { 51 | return _optimize; 52 | } 53 | 54 | void optimize(bool value) 55 | { 56 | _optimize = value; 57 | } 58 | }; 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/atn_state.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | #include 6 | 7 | #include "atn_state.hpp" 8 | #include "transition.hpp" 9 | 10 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 11 | #undef assert 12 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 13 | #endif 14 | 15 | namespace antlr4 { 16 | namespace atn { 17 | 18 | void atn_state::add_transition(std::shared_ptr const& transition) 19 | { 20 | add_transition(_transitions.size(), transition); 21 | } 22 | 23 | void atn_state::add_transition(size_t index, std::shared_ptr const& transition) 24 | { 25 | if (_transitions.empty()) 26 | { 27 | _epsilon_only_transitions = transition->epsilon(); 28 | } 29 | else if (_epsilon_only_transitions != transition->epsilon()) 30 | { 31 | assert(!"ATN state cannot have both epsilon and non-epsilon transitions."); 32 | _epsilon_only_transitions = false; 33 | } 34 | 35 | _transitions.insert(_transitions.begin() + static_cast(index), transition); 36 | } 37 | 38 | void atn_state::set_transition(size_t index, std::shared_ptr const& transition) 39 | { 40 | _transitions[index] = transition; 41 | } 42 | 43 | std::shared_ptr atn_state::remove_transition(size_t index) 44 | { 45 | std::shared_ptr result(std::move(_transitions.at(index))); 46 | _transitions.erase(_transitions.begin() + static_cast(index)); 47 | return std::move(result); 48 | } 49 | 50 | void atn_state::add_optimized_transition(size_t index, std::shared_ptr const& transition) 51 | { 52 | _optimized = true; 53 | _optimized_transitions.insert(_optimized_transitions.begin() + static_cast(index), transition); 54 | } 55 | 56 | void atn_state::set_optimized_transition(size_t index, std::shared_ptr const& transition) 57 | { 58 | assert(optimized()); 59 | _optimized_transitions[index] = transition; 60 | } 61 | 62 | std::shared_ptr atn_state::remove_optimized_transition(size_t index) 63 | { 64 | assert(optimized()); 65 | std::shared_ptr result(std::move(_optimized_transitions.at(index))); 66 | _optimized_transitions.erase(_optimized_transitions.begin() + static_cast(index)); 67 | return std::move(result); 68 | } 69 | 70 | std::shared_ptr star_loopback_state::loop_entry_state() const 71 | { 72 | return std::static_pointer_cast(transition(0)->target()); 73 | } 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/atn_state.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace antlr4 { 10 | namespace atn { 11 | 12 | class transition; 13 | 14 | class atn_state abstract 15 | { 16 | public: 17 | enum class atn_state_type 18 | { 19 | invalid, 20 | basic, 21 | rule_start, 22 | block_start, 23 | plus_block_start, 24 | star_block_start, 25 | token_start, 26 | rule_stop, 27 | block_end, 28 | star_loop_back, 29 | star_loop_entry, 30 | plus_loop_back, 31 | loop_end, 32 | }; 33 | 34 | private: 35 | const atn_state_type _state_type; 36 | const size_t _state_number; 37 | const size_t _rule_index; 38 | 39 | bool _epsilon_only_transitions; 40 | bool _optimized; 41 | 42 | std::vector> _transitions; 43 | std::vector> _optimized_transitions; 44 | 45 | public: 46 | static const size_t invalid_state_number = ~static_cast(0); 47 | 48 | protected: 49 | atn_state(atn_state_type state_type, size_t state_number, size_t rule_index) 50 | : _state_type(state_type) 51 | , _state_number(state_number) 52 | , _rule_index(rule_index) 53 | , _epsilon_only_transitions() 54 | { 55 | } 56 | 57 | public: 58 | atn_state_type state_type() const 59 | { 60 | return _state_type; 61 | } 62 | 63 | size_t state_number() const 64 | { 65 | return _state_number; 66 | } 67 | 68 | size_t rule_index() const 69 | { 70 | return _rule_index; 71 | } 72 | 73 | bool only_has_epsilon_transitions() const 74 | { 75 | return _epsilon_only_transitions; 76 | } 77 | 78 | bool optimized() const 79 | { 80 | return _optimized; 81 | } 82 | 83 | std::vector> const& transitions() const 84 | { 85 | return _transitions; 86 | } 87 | 88 | std::shared_ptr const& transition(size_t index) const 89 | { 90 | return transitions()[index]; 91 | } 92 | 93 | std::vector> const& optimized_transitions() const 94 | { 95 | return optimized() ? _optimized_transitions : _transitions; 96 | } 97 | 98 | std::shared_ptr const& optimized_transition(size_t index) const 99 | { 100 | return optimized_transitions()[index]; 101 | } 102 | 103 | size_t non_stop_state_number() const 104 | { 105 | return state_type() == atn_state_type::rule_stop ? invalid_state_number : state_number(); 106 | } 107 | 108 | public: 109 | void add_transition(std::shared_ptr const& transition); 110 | void add_transition(size_t index, std::shared_ptr const& transition); 111 | void set_transition(size_t index, std::shared_ptr const& transition); 112 | std::shared_ptr remove_transition(size_t index); 113 | 114 | void add_optimized_transition(size_t index, std::shared_ptr const& transition); 115 | void set_optimized_transition(size_t index, std::shared_ptr const& transition); 116 | std::shared_ptr remove_optimized_transition(size_t index); 117 | }; 118 | 119 | inline bool operator== (atn_state const& x, atn_state const& y) 120 | { 121 | return x.state_number() == y.state_number(); 122 | } 123 | 124 | class basic_state; 125 | class block_end_state; 126 | class decision_state; 127 | class block_start_state; 128 | class basic_block_start_state; 129 | class plus_block_start_state; 130 | class star_block_start_state; 131 | class plus_loopback_state; 132 | class star_loop_entry_state; 133 | class tokens_start_state; 134 | class loop_end_state; 135 | class rule_start_state; 136 | class rule_stop_state; 137 | class star_loopback_state; 138 | 139 | class basic_state final : public atn_state 140 | { 141 | public: 142 | basic_state(size_t state_number, size_t rule_index) 143 | : atn_state(atn_state_type::basic, state_number, rule_index) 144 | { 145 | } 146 | }; 147 | 148 | class block_end_state : public atn_state 149 | { 150 | private: 151 | std::shared_ptr _start_state; 152 | 153 | public: 154 | block_end_state(size_t state_number, size_t rule_index) 155 | : atn_state(atn_state_type::block_end, state_number, rule_index) 156 | { 157 | } 158 | 159 | public: 160 | std::shared_ptr const& start_state() const 161 | { 162 | return _start_state; 163 | } 164 | 165 | void start_state(std::shared_ptr const& value) 166 | { 167 | _start_state = value; 168 | } 169 | }; 170 | 171 | class decision_state abstract : public atn_state 172 | { 173 | private: 174 | size_t _decision; 175 | bool _greedy; 176 | bool _sll; 177 | 178 | protected: 179 | decision_state(atn_state_type state_type, size_t state_number, size_t rule_index) 180 | : atn_state(state_type, state_number, rule_index) 181 | , _decision(~static_cast(0)) 182 | , _greedy(true) 183 | , _sll(false) 184 | { 185 | } 186 | 187 | public: 188 | size_t decision() const 189 | { 190 | return _decision; 191 | } 192 | 193 | void decision(size_t value) 194 | { 195 | _decision = value; 196 | } 197 | 198 | bool greedy() const 199 | { 200 | return _greedy; 201 | } 202 | 203 | void greedy(bool value) 204 | { 205 | _greedy = value; 206 | } 207 | 208 | bool sll() const 209 | { 210 | return _sll; 211 | } 212 | 213 | void sll(bool value) 214 | { 215 | _sll = value; 216 | } 217 | }; 218 | 219 | class block_start_state abstract : public decision_state 220 | { 221 | private: 222 | std::shared_ptr _end_state; 223 | 224 | protected: 225 | block_start_state(atn_state_type state_type, size_t state_number, size_t rule_index) 226 | : decision_state(state_type, state_number, rule_index) 227 | { 228 | } 229 | 230 | public: 231 | std::shared_ptr const& end_state() const 232 | { 233 | return _end_state; 234 | } 235 | 236 | void end_state(std::shared_ptr const& value) 237 | { 238 | _end_state = value; 239 | } 240 | }; 241 | 242 | class basic_block_start_state final : public block_start_state 243 | { 244 | public: 245 | basic_block_start_state(size_t state_number, size_t rule_index) 246 | : block_start_state(atn_state_type::block_start, state_number, rule_index) 247 | { 248 | } 249 | }; 250 | 251 | class plus_block_start_state final : public block_start_state 252 | { 253 | private: 254 | std::shared_ptr _loopback_state; 255 | 256 | public: 257 | plus_block_start_state(size_t state_number, size_t rule_index) 258 | : block_start_state(atn_state_type::plus_block_start, state_number, rule_index) 259 | { 260 | } 261 | 262 | public: 263 | std::shared_ptr const& loopback_state() const 264 | { 265 | return _loopback_state; 266 | } 267 | 268 | void loopback_state(std::shared_ptr const& value) 269 | { 270 | _loopback_state = value; 271 | } 272 | }; 273 | 274 | class star_block_start_state final : public block_start_state 275 | { 276 | public: 277 | star_block_start_state(size_t state_number, size_t rule_index) 278 | : block_start_state(atn_state_type::star_block_start, state_number, rule_index) 279 | { 280 | } 281 | }; 282 | 283 | class plus_loopback_state final : public decision_state 284 | { 285 | public: 286 | plus_loopback_state(size_t state_number, size_t rule_index) 287 | : decision_state(atn_state_type::plus_loop_back, state_number, rule_index) 288 | { 289 | } 290 | }; 291 | 292 | class star_loop_entry_state final : public decision_state 293 | { 294 | private: 295 | std::shared_ptr _loopback_state; 296 | bool _precedence_rule_decision; 297 | 298 | public: 299 | star_loop_entry_state(size_t state_number, size_t rule_index) 300 | : decision_state(atn_state_type::star_loop_entry, state_number, rule_index) 301 | , _precedence_rule_decision() 302 | { 303 | } 304 | 305 | public: 306 | std::shared_ptr const& loopback_state() const 307 | { 308 | return _loopback_state; 309 | } 310 | 311 | void loopback_state(std::shared_ptr const& value) 312 | { 313 | _loopback_state = value; 314 | } 315 | 316 | bool precedence_rule_decision() const 317 | { 318 | return _precedence_rule_decision; 319 | } 320 | 321 | void precedence_rule_decision(bool value) 322 | { 323 | _precedence_rule_decision = value; 324 | } 325 | }; 326 | 327 | class tokens_start_state final : public decision_state 328 | { 329 | public: 330 | tokens_start_state(size_t state_number, size_t rule_index) 331 | : decision_state(atn_state_type::token_start, state_number, rule_index) 332 | { 333 | } 334 | }; 335 | 336 | class loop_end_state final : public atn_state 337 | { 338 | private: 339 | std::shared_ptr _loopback_state; 340 | 341 | public: 342 | loop_end_state(size_t state_number, size_t rule_index) 343 | : atn_state(atn_state_type::loop_end, state_number, rule_index) 344 | { 345 | } 346 | 347 | public: 348 | std::shared_ptr const& loopback_state() const 349 | { 350 | return _loopback_state; 351 | } 352 | 353 | void loopback_state(std::shared_ptr const& value) 354 | { 355 | _loopback_state = value; 356 | } 357 | }; 358 | 359 | class rule_start_state final : public atn_state 360 | { 361 | private: 362 | std::shared_ptr _stop_state; 363 | bool _precedence_rule; 364 | bool _left_factored; 365 | 366 | public: 367 | rule_start_state(size_t state_number, size_t rule_index) 368 | : atn_state(atn_state_type::rule_start, state_number, rule_index) 369 | , _precedence_rule(false) 370 | , _left_factored(false) 371 | { 372 | } 373 | 374 | std::shared_ptr const& stop_state() const 375 | { 376 | return _stop_state; 377 | } 378 | 379 | void stop_state(std::shared_ptr const& value) 380 | { 381 | _stop_state = value; 382 | } 383 | 384 | bool precedence_rule() const 385 | { 386 | return _precedence_rule; 387 | } 388 | 389 | void precedence_rule(bool value) 390 | { 391 | _precedence_rule = value; 392 | } 393 | 394 | bool left_factored() const 395 | { 396 | return _left_factored; 397 | } 398 | 399 | void left_factored(bool value) 400 | { 401 | _left_factored = value; 402 | } 403 | }; 404 | 405 | class rule_stop_state final : public atn_state 406 | { 407 | public: 408 | rule_stop_state(size_t state_number, size_t rule_index) 409 | : atn_state(atn_state_type::rule_stop, state_number, rule_index) 410 | { 411 | } 412 | }; 413 | 414 | class star_loopback_state final : public atn_state 415 | { 416 | public: 417 | star_loopback_state(size_t state_number, size_t rule_index) 418 | : atn_state(atn_state_type::star_loop_back, state_number, rule_index) 419 | { 420 | } 421 | 422 | public: 423 | std::shared_ptr loop_entry_state() const; 424 | }; 425 | 426 | } 427 | } 428 | 429 | namespace std { 430 | 431 | template<> 432 | class hash 433 | { 434 | size_t operator() (antlr4::atn::atn_state const& state) const 435 | { 436 | return state.state_number(); 437 | } 438 | }; 439 | 440 | } 441 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/atn_type.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr4 { 5 | namespace atn { 6 | 7 | // Represents the type of recognizer an ATN applies to. 8 | enum class atn_type 9 | { 10 | // A lexer grammar 11 | lexer, 12 | 13 | // A parser grammar 14 | parser, 15 | }; 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/conflict_information.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | namespace atn { 8 | 9 | bool operator== (conflict_information const& /*x*/, conflict_information const& /*y*/) 10 | { 11 | throw std::runtime_error("not implemented"); 12 | } 13 | 14 | } 15 | } 16 | 17 | namespace std { 18 | 19 | using namespace antlr4::atn; 20 | 21 | inline bool hash::operator()(conflict_information const& /*x*/) const 22 | { 23 | throw std::runtime_error("not implemented"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/conflict_information.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | namespace atn { 8 | 9 | class conflict_information 10 | { 11 | private: 12 | conflict_information() = delete; 13 | conflict_information(conflict_information const&) = delete; 14 | conflict_information& operator= (conflict_information const&) = delete; 15 | 16 | private: 17 | const std::vector _conflicted_alternatives; 18 | const bool _exact; 19 | 20 | public: 21 | conflict_information(std::vector&& conflicted_alternatives, bool exact) 22 | : _conflicted_alternatives(conflicted_alternatives) 23 | , _exact(exact) 24 | { 25 | } 26 | 27 | public: 28 | std::vector const& conflicted_alternatives() const 29 | { 30 | return _conflicted_alternatives; 31 | } 32 | 33 | bool exact() const 34 | { 35 | return _exact; 36 | } 37 | }; 38 | 39 | bool operator== (conflict_information const& x, conflict_information const& y); 40 | 41 | } 42 | } 43 | 44 | namespace std { 45 | 46 | template<> 47 | struct hash 48 | { 49 | bool operator() (antlr4::atn::conflict_information const& x) const; 50 | }; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/lexer_action.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | #include 7 | 8 | namespace antlr4 { 9 | namespace atn { 10 | 11 | namespace { 12 | 13 | class concrete_more_action : public lexer_action::more_action 14 | { 15 | private: 16 | concrete_more_action(concrete_more_action const&) = delete; 17 | concrete_more_action& operator= (concrete_more_action const&) = delete; 18 | 19 | public: 20 | explicit concrete_more_action() 21 | { 22 | } 23 | }; 24 | 25 | class concrete_pop_mode_action : public lexer_action::pop_mode_action 26 | { 27 | private: 28 | concrete_pop_mode_action(concrete_pop_mode_action const&) = delete; 29 | concrete_pop_mode_action& operator= (concrete_pop_mode_action const&) = delete; 30 | 31 | public: 32 | explicit concrete_pop_mode_action() 33 | { 34 | } 35 | }; 36 | 37 | class concrete_skip_action : public lexer_action::skip_action 38 | { 39 | private: 40 | concrete_skip_action(concrete_skip_action const&) = delete; 41 | concrete_skip_action& operator= (concrete_skip_action const&) = delete; 42 | 43 | public: 44 | explicit concrete_skip_action() 45 | { 46 | } 47 | }; 48 | 49 | } 50 | 51 | const std::shared_ptr lexer_action::more_action::instance 52 | = std::make_shared(); 53 | 54 | const std::shared_ptr lexer_action::pop_mode_action::instance 55 | = std::make_shared(); 56 | 57 | const std::shared_ptr lexer_action::skip_action::instance 58 | = std::make_shared(); 59 | 60 | lexer_action::more_action::more_action() 61 | : lexer_action(lexer_action_type::more) 62 | { 63 | } 64 | 65 | lexer_action::pop_mode_action::pop_mode_action() 66 | : lexer_action(lexer_action_type::pop_mode) 67 | { 68 | } 69 | 70 | lexer_action::skip_action::skip_action() 71 | : lexer_action(lexer_action_type::skip) 72 | { 73 | } 74 | 75 | bool operator== (lexer_action const& /*x*/, lexer_action const& /*y*/) 76 | { 77 | throw std::runtime_error("not implemented"); 78 | } 79 | 80 | } 81 | } 82 | 83 | namespace std { 84 | 85 | using namespace antlr4::atn; 86 | 87 | size_t hash::operator() (lexer_action const& /*action*/) const 88 | { 89 | throw std::runtime_error("not implemented"); 90 | }; 91 | 92 | } 93 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/lexer_action.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | namespace atn { 8 | 9 | class lexer_action 10 | { 11 | public: 12 | enum class lexer_action_type 13 | { 14 | // 15 | // position independent 16 | // 17 | channel, 18 | mode, 19 | more, 20 | pop_mode, 21 | push_mode, 22 | skip, 23 | type, 24 | 25 | // 26 | // position dependent 27 | // 28 | custom, 29 | indexed_custom, 30 | }; 31 | 32 | private: 33 | const lexer_action_type _type; 34 | 35 | protected: 36 | lexer_action(lexer_action_type type) 37 | : _type(type) 38 | { 39 | } 40 | 41 | public: 42 | lexer_action_type action_type() const 43 | { 44 | return _type; 45 | } 46 | 47 | bool position_dependent() const 48 | { 49 | return action_type() >= lexer_action_type::custom; 50 | } 51 | 52 | public: 53 | class channel_action; 54 | class mode_action; 55 | class more_action; 56 | class pop_mode_action; 57 | class push_mode_action; 58 | class skip_action; 59 | class type_action; 60 | class custom_action; 61 | class indexed_custom_action; 62 | }; 63 | 64 | class lexer_action::channel_action : public lexer_action 65 | { 66 | private: 67 | channel_action(channel_action const&) = delete; 68 | channel_action& operator= (channel_action const&) = delete; 69 | 70 | private: 71 | const int32_t _channel; 72 | 73 | public: 74 | explicit channel_action(int32_t channel) 75 | : lexer_action(lexer_action_type::channel) 76 | , _channel(channel) 77 | { 78 | } 79 | 80 | int32_t channel() const 81 | { 82 | return _channel; 83 | } 84 | }; 85 | 86 | class lexer_action::mode_action : public lexer_action 87 | { 88 | private: 89 | mode_action(mode_action const&) = delete; 90 | mode_action& operator= (mode_action const&) = delete; 91 | 92 | private: 93 | const int32_t _mode; 94 | 95 | public: 96 | explicit mode_action(int32_t mode) 97 | : lexer_action(lexer_action_type::mode) 98 | , _mode(mode) 99 | { 100 | } 101 | 102 | int32_t mode() const 103 | { 104 | return _mode; 105 | } 106 | }; 107 | 108 | class lexer_action::more_action : public lexer_action 109 | { 110 | private: 111 | more_action(more_action const&) = delete; 112 | more_action& operator= (more_action const&) = delete; 113 | 114 | protected: 115 | explicit more_action(); 116 | 117 | public: 118 | static const std::shared_ptr instance; 119 | }; 120 | 121 | class lexer_action::pop_mode_action : public lexer_action 122 | { 123 | private: 124 | pop_mode_action(pop_mode_action const&) = delete; 125 | pop_mode_action& operator= (pop_mode_action const&) = delete; 126 | 127 | protected: 128 | explicit pop_mode_action(); 129 | 130 | public: 131 | static const std::shared_ptr instance; 132 | }; 133 | 134 | class lexer_action::push_mode_action : public lexer_action 135 | { 136 | private: 137 | push_mode_action(push_mode_action const&) = delete; 138 | push_mode_action& operator= (push_mode_action const&) = delete; 139 | 140 | private: 141 | const int32_t _mode; 142 | 143 | public: 144 | explicit push_mode_action(int32_t mode) 145 | : lexer_action(lexer_action_type::push_mode) 146 | , _mode(mode) 147 | { 148 | } 149 | 150 | int32_t mode() const 151 | { 152 | return _mode; 153 | } 154 | }; 155 | 156 | class lexer_action::skip_action : public lexer_action 157 | { 158 | private: 159 | skip_action(skip_action const&) = delete; 160 | skip_action& operator= (skip_action const&) = delete; 161 | 162 | protected: 163 | explicit skip_action(); 164 | 165 | public: 166 | static const std::shared_ptr instance; 167 | }; 168 | 169 | class lexer_action::type_action : public lexer_action 170 | { 171 | private: 172 | type_action(type_action const&) = delete; 173 | type_action& operator= (type_action const&) = delete; 174 | 175 | private: 176 | const int32_t _type; 177 | 178 | public: 179 | explicit type_action(int32_t type) 180 | : lexer_action(lexer_action_type::type) 181 | , _type(type) 182 | { 183 | } 184 | 185 | int32_t type() const 186 | { 187 | return _type; 188 | } 189 | }; 190 | 191 | class lexer_action::custom_action : public lexer_action 192 | { 193 | private: 194 | custom_action(custom_action const&) = delete; 195 | custom_action& operator= (custom_action const&) = delete; 196 | 197 | private: 198 | const size_t _rule_index; 199 | const size_t _action_index; 200 | 201 | public: 202 | explicit custom_action(size_t rule_index, size_t action_index) 203 | : lexer_action(lexer_action_type::custom) 204 | , _rule_index(rule_index) 205 | , _action_index(action_index) 206 | { 207 | } 208 | 209 | size_t rule_index() const 210 | { 211 | return _rule_index; 212 | } 213 | 214 | size_t action_index() const 215 | { 216 | return _action_index; 217 | } 218 | }; 219 | 220 | class lexer_action::indexed_custom_action : public lexer_action 221 | { 222 | private: 223 | indexed_custom_action(indexed_custom_action const&) = delete; 224 | indexed_custom_action& operator= (indexed_custom_action const&) = delete; 225 | 226 | private: 227 | const size_t _offset; 228 | const std::shared_ptr _action; 229 | 230 | public: 231 | indexed_custom_action(size_t offset, std::shared_ptr const& action) 232 | : lexer_action(lexer_action_type::indexed_custom) 233 | , _offset(offset) 234 | , _action(action) 235 | { 236 | } 237 | 238 | public: 239 | size_t offset() const 240 | { 241 | return _offset; 242 | } 243 | 244 | std::shared_ptr const& action() const 245 | { 246 | return _action; 247 | } 248 | }; 249 | 250 | bool operator== (lexer_action const& x, lexer_action const& y); 251 | 252 | } 253 | } 254 | 255 | namespace std { 256 | 257 | template<> 258 | struct hash 259 | { 260 | size_t operator() (antlr4::atn::lexer_action const& action) const; 261 | }; 262 | 263 | } 264 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/lexer_action_executor.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace antlr4 { 14 | namespace atn { 15 | 16 | using namespace antlr4::misc; 17 | 18 | namespace { 19 | 20 | size_t calculate_hash_code(std::vector> const& actions) 21 | { 22 | int32_t hash = murmur_hash::initialize(); 23 | for each (std::shared_ptr action in actions) 24 | { 25 | hash = murmur_hash::update(hash, action, ptr_hash>()); 26 | } 27 | 28 | return static_cast(murmur_hash::finish(hash, actions.size())); 29 | } 30 | 31 | } 32 | 33 | lexer_action_executor::lexer_action_executor(std::vector>&& actions) 34 | : _actions(actions) 35 | , _hash_code(calculate_hash_code(_actions)) 36 | { 37 | } 38 | 39 | std::shared_ptr lexer_action_executor::append(std::shared_ptr const& executor, std::shared_ptr const& action) 40 | { 41 | if (!executor) 42 | { 43 | return std::make_shared(std::vector> { action }); 44 | } 45 | 46 | std::vector> actions; 47 | actions.reserve(executor->actions().size() + 1); 48 | std::copy(executor->actions().begin(), executor->actions().end(), std::back_inserter(actions)); 49 | actions.push_back(action); 50 | return std::make_shared(std::move(actions)); 51 | } 52 | 53 | std::shared_ptr lexer_action_executor::fix_offset_before_match(std::shared_ptr const& executor, size_t offset) 54 | { 55 | std::vector> updated_actions; 56 | for (size_t i = 0; i < executor->actions().size(); i++) 57 | { 58 | if (executor->actions()[i]->position_dependent() && executor->actions()[i]->action_type() != lexer_action::lexer_action_type::indexed_custom) 59 | { 60 | if (updated_actions.empty()) 61 | { 62 | updated_actions.insert(updated_actions.end(), executor->actions().begin(), executor->actions().end()); 63 | } 64 | 65 | updated_actions[i] = std::make_shared(offset, updated_actions[i]); 66 | } 67 | } 68 | 69 | if (updated_actions.empty()) 70 | { 71 | return executor; 72 | } 73 | 74 | return std::make_shared(std::move(updated_actions)); 75 | } 76 | 77 | bool operator==(lexer_action_executor const& x, lexer_action_executor const& y) 78 | { 79 | if (&x == &y) 80 | { 81 | return true; 82 | } 83 | 84 | if (x._hash_code != y._hash_code) 85 | { 86 | return false; 87 | } 88 | 89 | if (x.actions().size() != y.actions().size()) 90 | { 91 | return false; 92 | } 93 | 94 | return std::equal(x.actions().begin(), x.actions().end(), y.actions().begin(), misc::ptr_equal_to>()); 95 | } 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/lexer_action_executor.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | namespace antlr4 { 8 | namespace atn { 9 | 10 | class lexer_action; 11 | 12 | class lexer_action_executor 13 | { 14 | private: 15 | std::vector> _actions; 16 | const size_t _hash_code; 17 | 18 | friend bool operator== (lexer_action_executor const&, lexer_action_executor const&); 19 | friend std::hash; 20 | 21 | public: 22 | lexer_action_executor(std::vector>&& actions); 23 | 24 | public: 25 | std::vector> const& actions() const 26 | { 27 | return _actions; 28 | } 29 | 30 | public: 31 | static std::shared_ptr append(std::shared_ptr const& executor, std::shared_ptr const& action); 32 | static std::shared_ptr fix_offset_before_match(std::shared_ptr const& executor, size_t offset); 33 | }; 34 | 35 | bool operator== (lexer_action_executor const& x, lexer_action_executor const& y); 36 | 37 | } 38 | } 39 | 40 | namespace std { 41 | 42 | template<> 43 | struct hash 44 | { 45 | size_t operator() (antlr4::atn::lexer_action_executor const& executor) const 46 | { 47 | return executor._hash_code; 48 | } 49 | }; 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/prediction_context.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 14 | #undef assert 15 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 16 | #endif 17 | 18 | namespace antlr4 { 19 | namespace atn { 20 | 21 | namespace { 22 | 23 | using misc::murmur_hash; 24 | typedef prediction_context_cache::identity_commutative_prediction_context_operands identity_commutative_prediction_context_operands; 25 | 26 | int32_t calculate_empty_hash_code(); 27 | 28 | struct concrete_prediction_context : prediction_context 29 | { 30 | public: 31 | explicit concrete_prediction_context() 32 | : prediction_context(calculate_empty_hash_code()) 33 | { 34 | } 35 | 36 | concrete_prediction_context(std::shared_ptr const& parent, int32_t return_state) 37 | : prediction_context(parent, return_state) 38 | { 39 | } 40 | 41 | concrete_prediction_context(std::vector>&& parents, std::vector&& return_states) 42 | : prediction_context(std::move(parents), std::move(return_states)) 43 | { 44 | } 45 | }; 46 | 47 | bool context_equal(prediction_context const& x, prediction_context const& y, std::deque>& self_work_list, std::deque>& other_work_list) 48 | { 49 | size_t self_size = x.size(); 50 | if (!self_size) 51 | { 52 | // only one instance of each case of the empty operands exists, so we just need to compare pointers 53 | if (!(&x == &y)) 54 | { 55 | return false; 56 | } 57 | 58 | return true; 59 | } 60 | 61 | size_t other_size = y.size(); 62 | if (self_size != other_size) 63 | { 64 | return false; 65 | } 66 | 67 | std::hash hasher; 68 | for (size_t i = 0; i < self_size; i++) 69 | { 70 | if (x.return_state(i) != y.return_state(i)) 71 | { 72 | return false; 73 | } 74 | 75 | auto self_parent = x.parent(i); 76 | auto other_parent = y.parent(i); 77 | if (self_parent == other_parent) 78 | { 79 | continue; 80 | } 81 | 82 | if (hasher(*self_parent) != hasher(*other_parent)) 83 | { 84 | return false; 85 | } 86 | 87 | self_work_list.push_back(std::move(self_parent)); 88 | other_work_list.push_back(std::move(other_parent)); 89 | } 90 | 91 | return true; 92 | } 93 | 94 | bool context_equal(prediction_context const& x, prediction_context const& y) 95 | { 96 | std::unordered_set visited; 97 | std::deque> self_work_list; 98 | std::deque> other_work_list; 99 | 100 | if (!context_equal(x, y, self_work_list, other_work_list)) 101 | { 102 | return false; 103 | } 104 | 105 | while (!self_work_list.empty()) 106 | { 107 | identity_commutative_prediction_context_operands operands(std::move(self_work_list.back()), std::move(other_work_list.back())); 108 | self_work_list.pop_back(); 109 | other_work_list.pop_back(); 110 | auto result = visited.insert(operands); 111 | if (!result.second) 112 | { 113 | continue; 114 | } 115 | 116 | if (!context_equal(*operands.x(), *operands.y(), self_work_list, other_work_list)) 117 | { 118 | return false; 119 | } 120 | } 121 | 122 | return true; 123 | } 124 | 125 | const int32_t initial_hash = 1; 126 | 127 | int32_t calculate_empty_hash_code() 128 | { 129 | int32_t hash = murmur_hash::initialize(initial_hash); 130 | hash = murmur_hash::finish(hash, 0); 131 | return hash; 132 | } 133 | 134 | int32_t calculate_hash_code(std::shared_ptr const& parent, int32_t return_state) 135 | { 136 | int32_t hash = murmur_hash::initialize(initial_hash); 137 | hash = murmur_hash::update(hash, parent); 138 | hash = murmur_hash::update(hash, return_state); 139 | hash = murmur_hash::finish(hash, 2); 140 | return hash; 141 | } 142 | 143 | int32_t calculate_hash_code(std::vector> const& parents, std::vector const& return_states) 144 | { 145 | int32_t hash = murmur_hash::initialize(initial_hash); 146 | for each (std::shared_ptr const& parent in parents) 147 | { 148 | hash = murmur_hash::update(hash, parent); 149 | } 150 | 151 | for each (int32_t return_state in return_states) 152 | { 153 | hash = murmur_hash::update(hash, return_state); 154 | } 155 | 156 | hash = murmur_hash::finish(hash, 2 * parents.size()); 157 | return hash; 158 | } 159 | 160 | // The default hash function for pointers is reference equality. Since C++ does not move objects in memory, we 161 | // don't need to provide the specialized hash mechanism here to avoid the computation of an identity hash code 162 | // like the Java code needs. 163 | typedef std::unordered_map, std::shared_ptr> identity_map; 164 | 165 | std::shared_ptr append_context_impl(std::shared_ptr const& context, std::shared_ptr const& suffix, identity_map& visited) 166 | { 167 | if (suffix->is_empty()) 168 | { 169 | if (suffix->is_empty_local()) 170 | { 171 | if (context->has_empty()) 172 | { 173 | return prediction_context::empty_local; 174 | } 175 | 176 | // This is also not implemented in the Java code. 177 | throw std::runtime_error("what to do here?"); 178 | } 179 | 180 | return context; 181 | } 182 | 183 | if (suffix->size() != 1) 184 | { 185 | throw std::runtime_error("Appending a tree suffix is not yet supported."); 186 | } 187 | 188 | auto result_it = visited.find(context); 189 | if (result_it == visited.end()) 190 | { 191 | std::shared_ptr result; 192 | if (context->is_empty()) 193 | { 194 | result = suffix; 195 | } 196 | else 197 | { 198 | size_t parent_count = context->size(); 199 | if (context->has_empty()) 200 | { 201 | parent_count--; 202 | } 203 | 204 | std::vector> updated_parents(parent_count); 205 | std::vector updated_return_states(parent_count); 206 | for (size_t i = 0; i < parent_count; i++) 207 | { 208 | // this loop could be improved with access to the prediction_context::return_states 209 | updated_return_states[i] = context->return_state(i); 210 | } 211 | 212 | for (size_t i = 0; i < parent_count; i++) 213 | { 214 | updated_parents[i] = append_context_impl(context->parent(i), suffix, visited); 215 | } 216 | 217 | result = std::make_shared(std::move(updated_parents), std::move(updated_return_states)); 218 | 219 | if (context->has_empty()) 220 | { 221 | prediction_context_cache context_cache(prediction_context_cache::uncached()); 222 | result = prediction_context::join(result, suffix, context_cache); 223 | } 224 | } 225 | 226 | result_it = visited.insert(std::make_pair(context, result)).first; 227 | } 228 | 229 | return result_it->second; 230 | } 231 | 232 | } 233 | 234 | const std::shared_ptr prediction_context::empty_local(std::make_shared()); 235 | const std::shared_ptr prediction_context::empty_full(std::make_shared()); 236 | 237 | prediction_context::prediction_context(int32_t cached_hash_code) 238 | : cached_hash_code(cached_hash_code) 239 | { 240 | } 241 | 242 | prediction_context::prediction_context(std::shared_ptr const& parent, int32_t return_state) 243 | : cached_hash_code(calculate_hash_code(parent, return_state)) 244 | , parents(1) 245 | , return_states(1) 246 | { 247 | parents[0] = parent; 248 | return_states[0] = return_state; 249 | } 250 | 251 | prediction_context::prediction_context(std::vector>&& parents, std::vector&& return_states) 252 | : cached_hash_code(calculate_hash_code(parents, return_states)) 253 | , parents(parents) 254 | , return_states(return_states) 255 | { 256 | assert(this->parents.size() == this->return_states.size()); 257 | } 258 | 259 | size_t prediction_context::find_return_state(int32_t return_state) const 260 | { 261 | auto bound = std::lower_bound(std::cbegin(return_states), std::cend(return_states), return_state); 262 | return static_cast(bound - return_states.begin()); 263 | } 264 | 265 | std::shared_ptr prediction_context::add_empty_context(std::shared_ptr const& context) 266 | { 267 | if (context->has_empty()) 268 | { 269 | return context; 270 | } 271 | 272 | auto parents(context->parents); 273 | parents.push_back(empty_full); 274 | auto return_states(context->return_states); 275 | return_states.push_back(empty_full_state_key); 276 | 277 | return std::make_shared(std::move(parents), std::move(return_states)); 278 | } 279 | 280 | std::shared_ptr prediction_context::remove_empty_context(std::shared_ptr const& context) 281 | { 282 | if (!context->has_empty()) 283 | { 284 | return context; 285 | } 286 | 287 | auto parents(context->parents); 288 | parents.pop_back(); 289 | auto return_states(context->return_states); 290 | return_states.pop_back(); 291 | 292 | return std::make_shared(std::move(parents), std::move(return_states)); 293 | } 294 | 295 | std::shared_ptr prediction_context::append_context(std::shared_ptr const& context, int32_t return_context, prediction_context_cache& context_cache) 296 | { 297 | return append_context(context, get_child(empty_full, return_context), context_cache); 298 | } 299 | 300 | std::shared_ptr prediction_context::append_context(std::shared_ptr const& context, std::shared_ptr const& suffix, prediction_context_cache& context_cache) 301 | { 302 | if (context->is_empty()) 303 | { 304 | return suffix; 305 | } 306 | 307 | if (context->size() == 1) 308 | { 309 | return context_cache.get_child(append_context(context->parents[0], suffix, context_cache), context->return_states[0]); 310 | } 311 | 312 | identity_map visited; 313 | return append_context_impl(context, suffix, visited); 314 | } 315 | 316 | std::shared_ptr prediction_context::get_child(std::shared_ptr const& context, int32_t return_state) 317 | { 318 | return std::make_shared(context, return_state); 319 | } 320 | 321 | std::shared_ptr prediction_context::from_rule_context(std::shared_ptr const& /*atn*/, std::shared_ptr const& /*outer_context*/, bool /*full_context*/) 322 | { 323 | throw std::runtime_error("Not implemented"); 324 | //if (outer_context->is_empty()) 325 | //{ 326 | // return full_context ? empty_full : empty_local; 327 | //} 328 | 329 | //std::shared_ptr parent; 330 | //if (outer_context->parent()) 331 | //{ 332 | // parent = from_rule_context(atn, outer_context->parent(), full_context); 333 | //} 334 | //else 335 | //{ 336 | // parent = full_context ? empty_full : empty_local; 337 | //} 338 | 339 | //auto state = atn->states().find(outer_context->invoking_state); 340 | //auto transition = static_cast(state->transition(0)); 341 | //return get_child(parent, transition->follow_state()->state_number()); 342 | } 343 | 344 | std::shared_ptr prediction_context::join(std::shared_ptr const& context0, std::shared_ptr const& context1, prediction_context_cache& context_cache) 345 | { 346 | if (context0 == context1) 347 | { 348 | return context0; 349 | } 350 | 351 | if (context0->is_empty()) 352 | { 353 | return context0->is_empty_local() ? context0 : add_empty_context(context1); 354 | } 355 | else if (context1->is_empty()) 356 | { 357 | return context1->is_empty_local() ? context1 : add_empty_context(context0); 358 | } 359 | 360 | const size_t context0_size = context0->size(); 361 | const size_t context1_size = context1->size(); 362 | if (context0_size == 1 && context1_size == 1 && context0->return_state(0) == context1->return_state(0)) 363 | { 364 | std::shared_ptr merged = context_cache.join(context0->parent(0), context1->parent(0)); 365 | if (merged == context0->parent(0)) 366 | { 367 | return context0; 368 | } 369 | else if (merged == context1->parent(0)) 370 | { 371 | return context1; 372 | } 373 | else 374 | { 375 | return get_child(merged, context0->return_state(0)); 376 | } 377 | } 378 | 379 | size_t count = 0; 380 | std::vector> parents_list; 381 | std::vector return_states_list; 382 | parents_list.reserve(context0_size + context1_size); 383 | return_states_list.reserve(context0_size + context1_size); 384 | size_t left_index = 0; 385 | size_t right_index = 0; 386 | bool can_return_left = true; 387 | bool can_return_right = true; 388 | while (left_index < context0_size && right_index < context1_size) 389 | { 390 | if (context0->return_state(left_index) == context1->return_state(right_index)) 391 | { 392 | parents_list.push_back(context_cache.join(context0->parent(left_index), context1->parent(right_index))); 393 | return_states_list.push_back(context0->return_state(left_index)); 394 | can_return_left &= parents_list[count] == context0->parent(left_index); 395 | can_return_right &= parents_list[count] == context1->parent(right_index); 396 | left_index++; 397 | right_index++; 398 | } 399 | else if (context0->return_state(left_index) < context1->return_state(right_index)) 400 | { 401 | parents_list.push_back(context0->parent(left_index)); 402 | return_states_list.push_back(context0->return_state(left_index)); 403 | can_return_right = false; 404 | left_index++; 405 | } 406 | else 407 | { 408 | assert(context1->return_state(right_index) < context0->return_state(left_index)); 409 | parents_list.push_back(context1->parent(right_index)); 410 | return_states_list.push_back(context1->return_state(right_index)); 411 | can_return_left = false; 412 | right_index++; 413 | } 414 | 415 | count++; 416 | } 417 | 418 | while (left_index < context0_size) 419 | { 420 | parents_list.push_back(context0->parent(left_index)); 421 | return_states_list.push_back(context0->return_state(left_index)); 422 | left_index++; 423 | can_return_right = false; 424 | count++; 425 | } 426 | 427 | while (right_index < context1_size) 428 | { 429 | parents_list.push_back(context1->parent(right_index)); 430 | return_states_list.push_back(context1->return_state(right_index)); 431 | right_index++; 432 | can_return_left = false; 433 | count++; 434 | } 435 | 436 | if (can_return_left) 437 | { 438 | return context0; 439 | } 440 | else if (can_return_right) 441 | { 442 | return context1; 443 | } 444 | 445 | parents_list.shrink_to_fit(); 446 | return_states_list.shrink_to_fit(); 447 | 448 | if (parents_list.empty()) 449 | { 450 | // if one of them was `empty_local`, it would be empty and handled at the beginning of the method 451 | return empty_full; 452 | } 453 | else 454 | { 455 | return std::make_shared(std::move(parents_list), std::move(return_states_list)); 456 | } 457 | } 458 | 459 | bool operator== (prediction_context const& x, prediction_context const& y) 460 | { 461 | if (&x == &y) 462 | return true; 463 | 464 | if (x.cached_hash_code != y.cached_hash_code) 465 | return false; 466 | 467 | return context_equal(x, y); 468 | } 469 | 470 | } 471 | } 472 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/prediction_context.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace antlr4 { 10 | 11 | class rule_context; 12 | 13 | namespace atn { 14 | 15 | class grammar_atn; 16 | class prediction_context_cache; 17 | 18 | class prediction_context 19 | { 20 | prediction_context() = delete; 21 | prediction_context(prediction_context const&) = delete; 22 | prediction_context& operator=(prediction_context const&) = delete; 23 | 24 | private: 25 | const int32_t cached_hash_code; 26 | friend std::hash; 27 | friend bool operator== (prediction_context const&, prediction_context const&); 28 | 29 | std::vector> parents; 30 | std::vector return_states; 31 | 32 | protected: 33 | prediction_context(int32_t cached_hash_code); 34 | prediction_context(std::shared_ptr const& parent, int32_t return_state); 35 | prediction_context(std::vector>&& parents, std::vector&& return_states); 36 | 37 | public: 38 | static const int32_t empty_local_state_key = ~static_cast(0); 39 | static const int32_t empty_full_state_key = empty_local_state_key - 1; 40 | 41 | static const std::shared_ptr empty_local; 42 | static const std::shared_ptr empty_full; 43 | 44 | public: 45 | size_t size() const 46 | { 47 | return parents.size(); 48 | } 49 | 50 | int32_t return_state(size_t index) const 51 | { 52 | return return_states[index]; 53 | } 54 | 55 | std::shared_ptr const& parent(size_t index) const 56 | { 57 | return parents[index]; 58 | } 59 | 60 | bool is_empty() const 61 | { 62 | return parents.empty(); 63 | } 64 | 65 | bool is_empty_local() const 66 | { 67 | return this == empty_local.get(); 68 | } 69 | 70 | bool has_empty() const 71 | { 72 | auto hashMethod = std::hash>(); 73 | return is_empty() || return_states[size() - 1] == empty_full_state_key; 74 | } 75 | 76 | size_t find_return_state(int32_t return_state) const; 77 | 78 | static std::shared_ptr add_empty_context(std::shared_ptr const& context); 79 | static std::shared_ptr remove_empty_context(std::shared_ptr const& context); 80 | static std::shared_ptr append_context(std::shared_ptr const& context, int32_t return_context, prediction_context_cache& context_cache); 81 | static std::shared_ptr append_context(std::shared_ptr const& context, std::shared_ptr const& suffix, prediction_context_cache& context_cache); 82 | static std::shared_ptr get_child(std::shared_ptr const& context, int32_t return_state); 83 | 84 | static std::shared_ptr from_rule_context(std::shared_ptr const& atn, std::shared_ptr const& outer_context, bool full_context = true); 85 | static std::shared_ptr join(std::shared_ptr const& context0, std::shared_ptr const& context1, prediction_context_cache& context_cache); 86 | }; 87 | 88 | bool operator== (prediction_context const& x, prediction_context const& y); 89 | 90 | } 91 | } 92 | 93 | namespace std { 94 | template<> 95 | struct hash { 96 | size_t operator()(antlr4::atn::prediction_context const& prediction_context) const 97 | { 98 | return static_cast(prediction_context.cached_hash_code); 99 | } 100 | }; 101 | } 102 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/prediction_context_cache.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | namespace antlr4 { 10 | namespace atn { 11 | 12 | namespace { 13 | 14 | class prediction_context_and_int 15 | { 16 | prediction_context_and_int() = delete; 17 | prediction_context_and_int(prediction_context_and_int const&) = delete; 18 | prediction_context_and_int& operator= (prediction_context_and_int const&) = delete; 19 | 20 | public: 21 | const std::shared_ptr context; 22 | const int32_t value; 23 | 24 | public: 25 | prediction_context_and_int(prediction_context_and_int&& other) 26 | : context(std::move(other.context)) 27 | , value(other.value) 28 | { 29 | } 30 | 31 | prediction_context_and_int(std::shared_ptr const& context, int32_t value) 32 | : context(context) 33 | , value(value) 34 | { 35 | } 36 | }; 37 | 38 | bool operator== (prediction_context_and_int const& x, prediction_context_and_int const& y) 39 | { 40 | if (&x == &y) 41 | { 42 | return true; 43 | } 44 | 45 | return x.value == y.value 46 | && (x.context == y.context || (x.context && y.context && *x.context == *y.context)); 47 | } 48 | 49 | } 50 | 51 | } 52 | } 53 | 54 | namespace std { 55 | 56 | template<> 57 | struct hash 58 | { 59 | size_t operator() (antlr4::atn::prediction_context_and_int const& value) const 60 | { 61 | size_t hash = 5; 62 | hash = 7 * hash + (value.context ? std::hash()(*value.context) : 0); 63 | hash = 7 * hash + value.value; 64 | return hash; 65 | } 66 | }; 67 | 68 | } 69 | 70 | namespace antlr4 { 71 | namespace atn { 72 | 73 | class prediction_context_cache::data 74 | { 75 | data(data const&) = delete; 76 | data& operator= (data const&) = delete; 77 | 78 | public: 79 | std::unordered_map, std::shared_ptr> contexts; 80 | std::unordered_map> child_contexts; 81 | std::unordered_map> join_contexts; 82 | 83 | public: 84 | data() 85 | { 86 | } 87 | }; 88 | 89 | prediction_context_cache::prediction_context_cache(bool enable_cache) 90 | : private_data(enable_cache ? std::move(std::make_unique()) : nullptr) 91 | { 92 | } 93 | 94 | prediction_context_cache::prediction_context_cache(prediction_context_cache && cache) 95 | : private_data(std::move(cache.private_data)) 96 | { 97 | } 98 | 99 | prediction_context_cache::~prediction_context_cache() 100 | { 101 | } 102 | 103 | prediction_context_cache prediction_context_cache::uncached() 104 | { 105 | return prediction_context_cache(); 106 | } 107 | 108 | std::shared_ptr prediction_context_cache::get_as_cached(std::shared_ptr const& context) 109 | { 110 | if (!private_data) 111 | { 112 | return context; 113 | } 114 | 115 | auto result = private_data->contexts.insert(std::make_pair(context, context)).first; 116 | return result->second; 117 | } 118 | 119 | std::shared_ptr prediction_context_cache::get_child(std::shared_ptr const& context, int return_state) 120 | { 121 | if (!private_data) 122 | { 123 | return prediction_context::get_child(context, return_state); 124 | } 125 | 126 | prediction_context_and_int operands(context, return_state); 127 | auto result = private_data->child_contexts.find(operands); 128 | if (result == private_data->child_contexts.end()) 129 | { 130 | auto child_context = get_as_cached(prediction_context::get_child(context, return_state)); 131 | result = private_data->child_contexts.insert(std::make_pair(std::move(operands), std::move(child_context))).first; 132 | } 133 | 134 | return result->second; 135 | } 136 | 137 | std::shared_ptr prediction_context_cache::join(std::shared_ptr const& x, std::shared_ptr const& y) 138 | { 139 | if (!private_data) 140 | { 141 | return prediction_context::join(x, y, *this); 142 | } 143 | 144 | auto operands = identity_commutative_prediction_context_operands(std::shared_ptr(x), std::shared_ptr(y)); 145 | auto result = private_data->join_contexts.find(operands); 146 | if (result != private_data->join_contexts.end()) 147 | { 148 | return result->second; 149 | } 150 | 151 | auto join_context = get_as_cached(prediction_context::join(x, y, *this)); 152 | result = private_data->join_contexts.insert(std::make_pair(std::move(operands), std::move(join_context))).first; 153 | return result->second; 154 | } 155 | 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/prediction_context_cache.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | namespace atn { 8 | 9 | class prediction_context; 10 | 11 | class prediction_context_cache 12 | { 13 | class data; 14 | 15 | prediction_context_cache(prediction_context_cache const&) = delete; 16 | prediction_context_cache& operator= (prediction_context_cache const&) = delete; 17 | 18 | private: 19 | std::unique_ptr private_data; 20 | 21 | public: 22 | prediction_context_cache(bool enable_cache = true); 23 | prediction_context_cache(prediction_context_cache&& cache); 24 | ~prediction_context_cache(); 25 | 26 | public: 27 | static prediction_context_cache uncached(); 28 | 29 | public: 30 | std::shared_ptr get_as_cached(std::shared_ptr const& context); 31 | std::shared_ptr get_child(std::shared_ptr const& context, int return_state); 32 | std::shared_ptr join(std::shared_ptr const& x, std::shared_ptr const& y); 33 | 34 | public: 35 | class identity_commutative_prediction_context_operands 36 | { 37 | private: 38 | const std::shared_ptr _x; 39 | const std::shared_ptr _y; 40 | 41 | public: 42 | identity_commutative_prediction_context_operands(std::shared_ptr && x, std::shared_ptr && y) 43 | : _x(x) 44 | , _y(y) 45 | { 46 | } 47 | 48 | std::shared_ptr const& x() const 49 | { 50 | return _x; 51 | } 52 | 53 | std::shared_ptr const& y() const 54 | { 55 | return _y; 56 | } 57 | }; 58 | }; 59 | 60 | inline bool operator== (prediction_context_cache::identity_commutative_prediction_context_operands const& x, prediction_context_cache::identity_commutative_prediction_context_operands const& y); 61 | 62 | } 63 | } 64 | 65 | namespace std { 66 | 67 | template<> 68 | struct hash 69 | { 70 | inline size_t operator()(antlr4::atn::prediction_context_cache::identity_commutative_prediction_context_operands const& x) const; 71 | }; 72 | 73 | } 74 | 75 | #include "prediction_context_cache.inl" 76 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/prediction_context_cache.inl: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "prediction_context_cache.hpp" 3 | #include "prediction_context.hpp" 4 | 5 | namespace antlr4 { 6 | namespace atn { 7 | 8 | inline bool operator== (prediction_context_cache::identity_commutative_prediction_context_operands const& x, prediction_context_cache::identity_commutative_prediction_context_operands const& y) 9 | { 10 | if (&x == &y) 11 | return true; 12 | 13 | return x.x() == y.x() && x.y() == y.y() 14 | || x.x() == y.y() && x.y() == y.x(); 15 | } 16 | 17 | } 18 | } 19 | 20 | namespace std { 21 | 22 | inline size_t std::hash::operator() (antlr4::atn::prediction_context_cache::identity_commutative_prediction_context_operands const& x) const 23 | { 24 | std::hash hasher; 25 | return hasher(*x.x()) ^ hasher(*x.y()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/semantic_context.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 16 | #undef assert 17 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 18 | #endif 19 | 20 | namespace antlr4 { 21 | namespace atn { 22 | 23 | using namespace antlr4::misc; 24 | 25 | namespace { 26 | 27 | typedef std::shared_ptr semantic_context_ptr; 28 | 29 | template 30 | std::vector> filter_precedence_predicates(Container& container) 31 | { 32 | std::vector> result; 33 | for (auto it = container.begin(); it != container.end(); /*empty*/) 34 | { 35 | if ((*it)->type() == semantic_context::context_type::precedence_predicate) 36 | { 37 | result.push_back(std::static_pointer_cast(*it)); 38 | container.erase(it++); 39 | } 40 | else 41 | { 42 | ++it; 43 | } 44 | } 45 | 46 | return std::move(result); 47 | } 48 | 49 | } 50 | 51 | const std::shared_ptr semantic_context::none = std::make_shared(-1, -1, false); 52 | 53 | std::shared_ptr semantic_context::combine_and(std::shared_ptr const& x, std::shared_ptr const& y) 54 | { 55 | if (!x || x == none) 56 | { 57 | return y; 58 | } 59 | 60 | if (!y || y == none) 61 | { 62 | return x; 63 | } 64 | 65 | auto result = std::make_shared(x, y); 66 | if (result->operands().size() == 1) 67 | { 68 | return result->operands()[0]; 69 | } 70 | 71 | return std::move(result); 72 | } 73 | 74 | std::shared_ptr semantic_context::combine_or(std::shared_ptr const& x, std::shared_ptr const& y) 75 | { 76 | if (!x) 77 | { 78 | return y; 79 | } 80 | 81 | if (!y) 82 | { 83 | return x; 84 | } 85 | 86 | if (x == none || y == none) 87 | { 88 | return none; 89 | } 90 | 91 | auto result = std::make_shared(x, y); 92 | if (result->operands().size() == 1) 93 | { 94 | return result->operands()[0]; 95 | } 96 | 97 | return std::move(result); 98 | } 99 | 100 | semantic_context::and_operator::and_operator(std::shared_ptr const& x, std::shared_ptr const& y) 101 | : semantic_context(context_type::and_operator) 102 | { 103 | unordered_ptr_set operands; 104 | if (x->type() == context_type::and_operator) 105 | { 106 | and_operator* x_and = static_cast(x.get()); 107 | operands.insert(x_and->operands().begin(), x_and->operands().end()); 108 | } 109 | else 110 | { 111 | operands.insert(x); 112 | } 113 | 114 | if (y->type() == context_type::and_operator) 115 | { 116 | and_operator* y_and = static_cast(y.get()); 117 | operands.insert(y_and->operands().begin(), y_and->operands().end()); 118 | } 119 | else 120 | { 121 | operands.insert(y); 122 | } 123 | 124 | std::vector> precedence_predicates(filter_precedence_predicates(operands)); 125 | if (!precedence_predicates.empty()) 126 | { 127 | // interested in the transition with the lowest precedence 128 | auto reduced = std::min_element( 129 | precedence_predicates.begin(), 130 | precedence_predicates.end(), 131 | [](std::shared_ptr const& x, std::shared_ptr const& y) 132 | { 133 | return x->precedence() < y->precedence(); 134 | }); 135 | operands.insert(*reduced); 136 | } 137 | 138 | std::copy(operands.begin(), operands.end(), std::back_inserter(_operands)); 139 | } 140 | 141 | semantic_context::or_operator::or_operator(std::shared_ptr const& x, std::shared_ptr const& y) 142 | : semantic_context(context_type::or_operator) 143 | { 144 | unordered_ptr_set operands; 145 | if (x->type() == context_type::or_operator) 146 | { 147 | or_operator* x_or = static_cast(x.get()); 148 | operands.insert(x_or->operands().begin(), x_or->operands().end()); 149 | } 150 | else 151 | { 152 | operands.insert(x); 153 | } 154 | 155 | if (y->type() == context_type::or_operator) 156 | { 157 | or_operator* y_or = static_cast(y.get()); 158 | operands.insert(y_or->operands().begin(), y_or->operands().end()); 159 | } 160 | else 161 | { 162 | operands.insert(y); 163 | } 164 | 165 | std::vector> precedence_predicates(filter_precedence_predicates(operands)); 166 | if (!precedence_predicates.empty()) 167 | { 168 | // interested in the transition with the highest precedence 169 | auto reduced = std::max_element( 170 | precedence_predicates.begin(), 171 | precedence_predicates.end(), 172 | [](std::shared_ptr const& x, std::shared_ptr const& y) 173 | { 174 | return x->precedence() < y->precedence(); 175 | }); 176 | operands.insert(*reduced); 177 | } 178 | 179 | std::copy(operands.begin(), operands.end(), std::back_inserter(_operands)); 180 | } 181 | 182 | bool operator== (semantic_context const& x, semantic_context const& y) 183 | { 184 | if (&x == &y) 185 | { 186 | return true; 187 | } 188 | 189 | if (x.type() != y.type()) 190 | { 191 | return false; 192 | } 193 | 194 | switch (x.type()) 195 | { 196 | case semantic_context::context_type::predicate: 197 | { 198 | semantic_context::predicate const& left = static_cast(x); 199 | semantic_context::predicate const& right = static_cast(y); 200 | return left.rule_index() == right.rule_index() 201 | && left.predicate_index() == right.predicate_index() 202 | && left.context_dependent() == right.context_dependent(); 203 | } 204 | 205 | case semantic_context::context_type::precedence_predicate: 206 | { 207 | semantic_context::precedence_predicate const& left = static_cast(x); 208 | semantic_context::precedence_predicate const& right = static_cast(y); 209 | return left.precedence() == right.precedence(); 210 | } 211 | 212 | case semantic_context::context_type::and_operator: 213 | { 214 | //semantic_context::and_operator const& left = static_cast(x); 215 | //semantic_context::and_operator const& right = static_cast(y); 216 | throw std::runtime_error("not implemented"); 217 | } 218 | 219 | case semantic_context::context_type::or_operator: 220 | { 221 | //semantic_context::or_operator const& left = static_cast(x); 222 | //semantic_context::or_operator const& right = static_cast(y); 223 | throw std::runtime_error("not implemented"); 224 | } 225 | 226 | default: 227 | assert(!"Invalid context type."); 228 | return false; 229 | } 230 | } 231 | 232 | } 233 | } 234 | 235 | namespace std { 236 | 237 | using namespace antlr4::atn; 238 | using namespace antlr4::misc; 239 | 240 | size_t hash::operator() (semantic_context const& x) const 241 | { 242 | switch (x.type()) 243 | { 244 | case semantic_context::context_type::predicate: 245 | { 246 | semantic_context::predicate const& left = static_cast(x); 247 | int32_t hash = murmur_hash::initialize(); 248 | hash = murmur_hash::update(hash, left.rule_index()); 249 | hash = murmur_hash::update(hash, left.predicate_index()); 250 | hash = murmur_hash::update(hash, left.context_dependent() ? 1 : 0); 251 | hash = murmur_hash::finish(hash, 3); 252 | return static_cast(hash); 253 | } 254 | 255 | case semantic_context::context_type::precedence_predicate: 256 | { 257 | semantic_context::precedence_predicate const& left = static_cast(x); 258 | int32_t hash = 1; 259 | hash = 31 * hash + left.precedence(); 260 | return static_cast(hash); 261 | } 262 | 263 | case semantic_context::context_type::and_operator: 264 | { 265 | //semantic_context::and_operator const& left = static_cast(x); 266 | throw std::runtime_error("not implemented"); 267 | } 268 | 269 | case semantic_context::context_type::or_operator: 270 | { 271 | //semantic_context::or_operator const& left = static_cast(x); 272 | throw std::runtime_error("not implemented"); 273 | } 274 | 275 | default: 276 | assert(!"Invalid context type."); 277 | return false; 278 | } 279 | } 280 | 281 | } 282 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/semantic_context.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | namespace antlr4 { 8 | namespace atn { 9 | 10 | class semantic_context 11 | { 12 | public: 13 | static const std::shared_ptr none; 14 | 15 | enum class context_type 16 | { 17 | predicate, 18 | precedence_predicate, 19 | and_operator, 20 | or_operator, 21 | }; 22 | 23 | private: 24 | const context_type _type; 25 | 26 | protected: 27 | semantic_context(context_type type) 28 | : _type(type) 29 | { 30 | } 31 | 32 | public: 33 | context_type type() const 34 | { 35 | return _type; 36 | } 37 | 38 | public: 39 | static std::shared_ptr combine_and(std::shared_ptr const& x, std::shared_ptr const& y); 40 | static std::shared_ptr combine_or(std::shared_ptr const& x, std::shared_ptr const& y); 41 | 42 | public: 43 | class predicate; 44 | class precedence_predicate; 45 | class and_operator; 46 | class or_operator; 47 | }; 48 | 49 | class semantic_context::predicate : public semantic_context 50 | { 51 | private: 52 | const int _rule_index; 53 | const int _predicate_index; 54 | const bool _context_dependent; 55 | 56 | public: 57 | predicate(int32_t rule_index, int32_t predicate_index, bool context_dependent) 58 | : semantic_context(context_type::predicate) 59 | , _rule_index(rule_index) 60 | , _predicate_index(predicate_index) 61 | , _context_dependent(context_dependent) 62 | { 63 | } 64 | 65 | int32_t rule_index() const 66 | { 67 | return _rule_index; 68 | } 69 | 70 | int32_t predicate_index() const 71 | { 72 | return _predicate_index; 73 | } 74 | 75 | bool context_dependent() const 76 | { 77 | return _context_dependent; 78 | } 79 | }; 80 | 81 | class semantic_context::precedence_predicate : public semantic_context 82 | { 83 | private: 84 | const int32_t _precedence; 85 | 86 | public: 87 | precedence_predicate(int precedence) 88 | : semantic_context(context_type::precedence_predicate) 89 | , _precedence(precedence) 90 | { 91 | } 92 | 93 | int32_t precedence() const 94 | { 95 | return _precedence; 96 | } 97 | }; 98 | 99 | class semantic_context::and_operator : public semantic_context 100 | { 101 | private: 102 | 103 | private: 104 | std::vector> _operands; 105 | 106 | public: 107 | and_operator(std::shared_ptr const& x, std::shared_ptr const& y); 108 | 109 | public: 110 | std::vector> const& operands() const 111 | { 112 | return _operands; 113 | } 114 | }; 115 | 116 | class semantic_context::or_operator : public semantic_context 117 | { 118 | private: 119 | 120 | private: 121 | std::vector> _operands; 122 | 123 | public: 124 | or_operator(std::shared_ptr const& x, std::shared_ptr const& y); 125 | 126 | public: 127 | std::vector> const& operands() const 128 | { 129 | return _operands; 130 | } 131 | }; 132 | 133 | bool operator== (semantic_context const& x, semantic_context const& y); 134 | 135 | } 136 | } 137 | 138 | namespace std { 139 | 140 | template<> 141 | struct hash 142 | { 143 | size_t operator() (antlr4::atn::semantic_context const& x) const; 144 | }; 145 | 146 | } 147 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/transition.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | #include 6 | 7 | #include "transition.hpp" 8 | 9 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 10 | #undef assert 11 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 12 | #endif 13 | 14 | namespace antlr4 { 15 | namespace atn { 16 | 17 | bool transition::matches(int32_t symbol, int32_t min_vocab, int32_t max_vocab) const 18 | { 19 | switch (type()) 20 | { 21 | case transition_type::epsilon: 22 | case transition_type::rule: 23 | case transition_type::predicate: 24 | case transition_type::action: 25 | case transition_type::precedence: 26 | return false; 27 | 28 | case transition_type::range: 29 | { 30 | range_transition const* transition = static_cast(this); 31 | std::pair label = transition->label(); 32 | return symbol >= label.first && symbol < label.second; 33 | } 34 | 35 | case transition_type::atom: 36 | { 37 | atom_transition const* transition = static_cast(this); 38 | return transition->label() == symbol; 39 | } 40 | 41 | case transition_type::set: 42 | { 43 | //set_transition const* transition = static_cast(this); 44 | throw std::runtime_error("not implemented"); 45 | } 46 | 47 | case transition_type::not_set: 48 | { 49 | //not_set_transition const* transition = static_cast(this); 50 | throw std::runtime_error("not implemented"); 51 | } 52 | 53 | case transition_type::wildcard: 54 | return symbol >= min_vocab && symbol <= max_vocab; 55 | 56 | default: 57 | assert(!"Invalid transition type."); 58 | return false; 59 | } 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/atn/transition.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include "semantic_context.hpp" 6 | 7 | namespace antlr4 { 8 | namespace atn { 9 | 10 | class atn_state; 11 | class rule_start_state; 12 | 13 | class transition { 14 | public: 15 | enum class transition_type 16 | { 17 | epsilon = 1, 18 | range, 19 | rule, 20 | predicate, 21 | atom, 22 | action, 23 | set, 24 | not_set, 25 | wildcard, 26 | precedence, 27 | }; 28 | 29 | private: 30 | const transition_type _type; 31 | const std::shared_ptr _target; 32 | 33 | public: 34 | transition(transition_type type, std::shared_ptr target) 35 | : _type(type) 36 | , _target(target) 37 | { 38 | } 39 | 40 | public: 41 | transition_type type() const 42 | { 43 | return _type; 44 | } 45 | 46 | std::shared_ptr const& target() const 47 | { 48 | return _target; 49 | } 50 | 51 | bool epsilon() const 52 | { 53 | switch (type()) 54 | { 55 | case transition_type::epsilon: 56 | case transition_type::rule: 57 | case transition_type::predicate: 58 | case transition_type::action: 59 | case transition_type::precedence: 60 | return true; 61 | 62 | case transition_type::range: 63 | case transition_type::atom: 64 | case transition_type::set: 65 | case transition_type::not_set: 66 | case transition_type::wildcard: 67 | default: 68 | return false; 69 | } 70 | } 71 | 72 | bool matches(int32_t symbol, int32_t min_vocab, int32_t max_vocab) const; 73 | }; 74 | 75 | class epsilon_transition : public transition 76 | { 77 | private: 78 | const int32_t _outermost_precedence_return; 79 | 80 | public: 81 | explicit epsilon_transition(std::shared_ptr const& target, int32_t outermost_precedence_return) 82 | : transition(transition_type::epsilon, target) 83 | , _outermost_precedence_return(outermost_precedence_return) 84 | { 85 | } 86 | 87 | public: 88 | int32_t outermost_precedence_return() const 89 | { 90 | return _outermost_precedence_return; 91 | } 92 | }; 93 | 94 | class range_transition : public transition 95 | { 96 | private: 97 | const std::pair _label; 98 | 99 | public: 100 | explicit range_transition(std::shared_ptr const& target, std::pair label) 101 | : transition(transition_type::range, target) 102 | , _label(label) 103 | { 104 | } 105 | 106 | public: 107 | std::pair label() const 108 | { 109 | return _label; 110 | } 111 | }; 112 | 113 | class rule_transition : public transition 114 | { 115 | private: 116 | const size_t _rule_index; 117 | const int32_t _precedence; 118 | const std::shared_ptr _follow_state; 119 | 120 | bool _tail_call; 121 | bool _optimized_tail_call; 122 | 123 | public: 124 | explicit rule_transition(std::shared_ptr const& target, size_t rule_index, int32_t precedence, std::shared_ptr const& follow_state) 125 | : transition(transition_type::rule, target) 126 | , _rule_index(rule_index) 127 | , _precedence(precedence) 128 | , _follow_state(follow_state) 129 | { 130 | } 131 | 132 | public: 133 | size_t rule_index() const 134 | { 135 | return _rule_index; 136 | } 137 | 138 | int32_t precedence() const 139 | { 140 | return _precedence; 141 | } 142 | 143 | std::shared_ptr const& follow_state() const 144 | { 145 | return _follow_state; 146 | } 147 | 148 | bool tail_call() const 149 | { 150 | return _tail_call; 151 | } 152 | 153 | bool optimized_tail_call() const 154 | { 155 | return _optimized_tail_call; 156 | } 157 | 158 | public: 159 | void tail_call(bool value) 160 | { 161 | _tail_call = value; 162 | } 163 | 164 | void optimized_tail_call(bool value) 165 | { 166 | _optimized_tail_call = value; 167 | } 168 | }; 169 | 170 | class predicate_transition : public transition 171 | { 172 | private: 173 | const size_t _rule_index; 174 | const size_t _predicate_index; 175 | const bool _context_dependent; 176 | 177 | public: 178 | explicit predicate_transition(std::shared_ptr const& target, size_t rule_index, size_t predicate_index, bool context_dependent) 179 | : transition(transition_type::predicate, target) 180 | , _rule_index(rule_index) 181 | , _predicate_index(predicate_index) 182 | , _context_dependent(context_dependent) 183 | { 184 | } 185 | 186 | public: 187 | size_t rule_index() const 188 | { 189 | return _rule_index; 190 | } 191 | 192 | size_t predicate_index() const 193 | { 194 | return _predicate_index; 195 | } 196 | 197 | bool context_dependent() const 198 | { 199 | return _context_dependent; 200 | } 201 | }; 202 | 203 | class atom_transition : public transition 204 | { 205 | private: 206 | const int32_t _label; 207 | 208 | public: 209 | explicit atom_transition(std::shared_ptr const& target, int32_t label) 210 | : transition(transition_type::atom, target) 211 | , _label(label) 212 | { 213 | } 214 | 215 | public: 216 | int32_t label() const 217 | { 218 | return _label; 219 | } 220 | }; 221 | 222 | class action_transition : public transition 223 | { 224 | private: 225 | const size_t _rule_index; 226 | const size_t _action_index; 227 | const bool _context_dependent; 228 | 229 | public: 230 | explicit action_transition(std::shared_ptr const& target, size_t rule_index, size_t action_index, bool context_dependent) 231 | : transition(transition_type::action, target) 232 | , _rule_index(rule_index) 233 | , _action_index(action_index) 234 | , _context_dependent(context_dependent) 235 | { 236 | } 237 | 238 | public: 239 | size_t rule_index() const 240 | { 241 | return _rule_index; 242 | } 243 | 244 | size_t action_index() const 245 | { 246 | return _action_index; 247 | } 248 | 249 | bool context_dependent() const 250 | { 251 | return _context_dependent; 252 | } 253 | }; 254 | 255 | class set_transition : public transition 256 | { 257 | public: 258 | explicit set_transition(std::shared_ptr const& target) 259 | : set_transition(transition_type::epsilon, target) 260 | { 261 | } 262 | 263 | protected: 264 | explicit set_transition(transition_type type, std::shared_ptr const& target) 265 | : transition(type, target) 266 | { 267 | throw std::runtime_error("not implemented"); 268 | } 269 | }; 270 | 271 | class not_set_transition : public set_transition 272 | { 273 | public: 274 | explicit not_set_transition(std::shared_ptr const& target) 275 | : set_transition(transition_type::not_set, target) 276 | { 277 | throw std::runtime_error("not implemented"); 278 | } 279 | }; 280 | 281 | class wildcard_transition : public transition 282 | { 283 | public: 284 | explicit wildcard_transition(std::shared_ptr const& target) 285 | : transition(transition_type::epsilon, target) 286 | { 287 | } 288 | }; 289 | 290 | class precedence_transition : public transition 291 | { 292 | private: 293 | const int32_t _precedence; 294 | 295 | public: 296 | explicit precedence_transition(std::shared_ptr const& target, int32_t precedence) 297 | : transition(transition_type::epsilon, target) 298 | , _precedence(precedence) 299 | { 300 | } 301 | 302 | public: 303 | int32_t precedence() const 304 | { 305 | return _precedence; 306 | } 307 | 308 | std::shared_ptr predicate() const 309 | { 310 | return std::make_shared(precedence()); 311 | } 312 | }; 313 | 314 | } 315 | } 316 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/dfa/accept_state_information.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | 8 | namespace atn { 9 | class lexer_action_executor; 10 | } 11 | 12 | namespace dfa { 13 | 14 | class accept_state_information 15 | { 16 | private: 17 | size_t _prediction; 18 | std::shared_ptr _lexer_action_executor; 19 | 20 | public: 21 | accept_state_information(size_t prediction) 22 | : _prediction(prediction) 23 | , _lexer_action_executor(nullptr) 24 | { 25 | } 26 | 27 | accept_state_information(size_t prediction, std::shared_ptr const& lexer_action_executor) 28 | : _prediction(prediction) 29 | , _lexer_action_executor(lexer_action_executor) 30 | { 31 | } 32 | 33 | public: 34 | size_t prediction() const 35 | { 36 | return _prediction; 37 | } 38 | 39 | std::shared_ptr const& lexer_action_executor() const 40 | { 41 | return _lexer_action_executor; 42 | } 43 | }; 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/interval_set.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "../token.hpp" 10 | #include "to_string.hpp" 11 | 12 | namespace antlr4 { 13 | namespace misc { 14 | 15 | template>> 16 | class interval_set 17 | { 18 | static_assert(std::is_integral<_Ty>::value, "interval_set only works with integers"); 19 | 20 | public: 21 | typedef _Ty value_type; 22 | typedef std::pair<_Ty, _Ty> interval_type; 23 | 24 | private: 25 | std::vector _pairs; 26 | 27 | public: 28 | interval_set() 29 | { 30 | } 31 | 32 | interval_set(interval_set const& set) 33 | : _pairs(set._pairs) 34 | { 35 | } 36 | 37 | interval_set(interval_set&& set) 38 | : _pairs(std::move(set.pairs())) 39 | { 40 | } 41 | 42 | interval_set& operator= (interval_set const& set) 43 | { 44 | _pairs = set.pairs(); 45 | } 46 | 47 | public: 48 | static interval_set of(_Ty value) 49 | { 50 | interval_set result; 51 | result.insert(value); 52 | return std::move(result); 53 | } 54 | 55 | static interval_set of(interval_type interval) 56 | { 57 | interval_set result; 58 | result.insert(interval); 59 | return std::move(result); 60 | } 61 | 62 | public: 63 | _Ty min() const 64 | { 65 | if (_pairs.empty()) 66 | { 67 | return 0; 68 | } 69 | 70 | return _pairs[0].first; 71 | } 72 | 73 | _Ty max() const 74 | { 75 | if (_pairs.empty()) 76 | { 77 | return 0; 78 | } 79 | 80 | return _pairs.back().second - 1; 81 | } 82 | 83 | std::vector const& pairs() const 84 | { 85 | return _pairs; 86 | } 87 | 88 | bool empty() const 89 | { 90 | return _pairs.empty(); 91 | } 92 | 93 | _Ty size() const 94 | { 95 | _Ty result = _Ty(); 96 | for each (interval_type const& pair in pairs()) 97 | { 98 | result += (pair.second - pair.first); 99 | } 100 | 101 | return result; 102 | } 103 | 104 | bool contains(_Ty value) const 105 | { 106 | // this could be greatly improved with a binary search 107 | for each (interval_type const& pair in pairs()) 108 | { 109 | if (value < pair.first) 110 | { 111 | // list is sorted and value is before this interval 112 | return false; 113 | } 114 | 115 | // NOTE: duplicate range check omitted 116 | if (/*value >= pair.first &&*/ value <= pair.second) 117 | { 118 | // found in this interval 119 | return true; 120 | } 121 | } 122 | 123 | return false; 124 | } 125 | 126 | public: 127 | void insert(_Ty value) 128 | { 129 | insert(std::make_pair(value, value + 1)); 130 | } 131 | 132 | void insert(interval_type range) 133 | { 134 | if (range.second <= range.first) 135 | { 136 | return; 137 | } 138 | 139 | for (auto iterator = _pairs.begin(); iterator != _pairs.end(); ++iterator) 140 | { 141 | interval_type pair = *iterator; 142 | if (range.first == pair.first && range.second == pair.second) 143 | { 144 | return; 145 | } 146 | 147 | bool mergeable = (range.first <= pair.second) && (range.second >= pair.first); 148 | if (mergeable) 149 | { 150 | // next to each other, make a single larger interval 151 | interval_type merged = std::make_pair(std::min(range.first, pair.first), std::max(range.second, pair.second)); 152 | *iterator = merged; 153 | 154 | // make sure we didn't just create an interval that should be merged with next interval in list 155 | while (++iterator != _pairs.end()) 156 | { 157 | pair = *iterator; 158 | mergeable = (merged.first <= pair.second) && (merged.second >= pair.first); 159 | if (!mergeable) 160 | { 161 | break; 162 | } 163 | 164 | // if we bump up against or overlap next, merge 165 | 166 | // remove this one 167 | iterator = _pairs.erase(iterator); 168 | // move backwards one element 169 | --iterator; 170 | // update to the union 171 | *iterator = std::make_pair(std::min(iterator->first, pair.first), std::max(iterator->second, pair.second)); 172 | } 173 | 174 | return; 175 | } 176 | else if (range.second < pair.first) 177 | { 178 | // insert before the current position 179 | _pairs.insert(iterator, range); 180 | return; 181 | } 182 | 183 | // if disjoint and after `pair`, a future iteration will handle it 184 | } 185 | 186 | // must be after last interval (and disjoint from last interval). just add it. 187 | _pairs.push_back(range); 188 | } 189 | 190 | void insert(interval_set const& set) 191 | { 192 | if (this == &set) 193 | { 194 | return; 195 | } 196 | 197 | for each (interval_type pair in set.pairs()) 198 | { 199 | insert(pair); 200 | } 201 | } 202 | 203 | void remove(_Ty value) 204 | { 205 | size_t n = pairs().size(); 206 | for (size_t i = 0; i < n; i++) 207 | { 208 | interval_type interval(_pairs[i]); 209 | _Ty a = interval.first; 210 | _Ty b = interval.second; 211 | if (value < a) 212 | { 213 | // list is sorted and el is before this interval; not here 214 | break; 215 | } 216 | 217 | if (value == a && value + 1 == b) 218 | { 219 | // if whole interval x..x, rm 220 | _pairs.erase(_pairs.begin() + static_cast(i)); 221 | break; 222 | } 223 | 224 | // if on left edge x..b, adjust left 225 | if (value == a) 226 | { 227 | _pairs[i] = std::make_pair(interval.first + 1, interval.second); 228 | break; 229 | } 230 | 231 | // if on right edge a..x, adjust right 232 | if (value + 1 == b) 233 | { 234 | _pairs[i] = std::make_pair(interval.first, interval.second - 1); 235 | break; 236 | } 237 | 238 | // if in middle a..x..b, split interval 239 | if (value > a && value + 1 < b) 240 | { 241 | // found in this interval 242 | _Ty old_b = interval.second; 243 | _pairs[i] = std::make_pair(interval.first, value); 244 | insert(std::make_pair(value + 1, old_b)); 245 | } 246 | } 247 | } 248 | 249 | public: 250 | static interval_set combine_or(interval_set const& x, interval_set const& y) 251 | { 252 | interval_set result(x); 253 | result.insert(y); 254 | return std::move(result); 255 | } 256 | 257 | static interval_set combine_and(interval_set const& x, interval_set const& y) 258 | { 259 | 260 | std::vector const& my_intervals = x.pairs(); 261 | std::vector const& their_intervals = y.pairs(); 262 | interval_set result; 263 | size_t my_size = my_intervals.size(); 264 | size_t their_size = their_intervals.size(); 265 | size_t i = 0; 266 | size_t j = 0; 267 | while (i < my_size && j < their_size) 268 | { 269 | interval_type mine = my_intervals[i]; 270 | interval_type theirs = their_intervals[j]; 271 | if (mine.second <= theirs.first) 272 | { 273 | // move this iterator looking for interval that might overlap 274 | i++; 275 | } 276 | else if (theirs.second <= mine.first) 277 | { 278 | // move other iterator looking for interval that might overlap 279 | j++; 280 | } 281 | else if (mine.first <= theirs.first && mine.second >= theirs.second) 282 | { 283 | // overlap, add intersection, get next theirs 284 | result.insert(theirs); 285 | j++; 286 | } 287 | else if (theirs.first <= mine.first && theirs.second >= mine.second) 288 | { 289 | // overlap, add intersection, get next mine 290 | result.insert(mine); 291 | i++; 292 | } 293 | else if (mine.first < theirs.second && mine.second > theirs.first) 294 | { 295 | result.insert(std::make_pair(std::max(mine.first, theirs.first), std::min(mine.second, theirs.second))); 296 | 297 | // Move the iterator of lower range [a..b], but not 298 | // the upper range as it may contain elements that will collide 299 | // with the next iterator. So, if mine=[0..115] and 300 | // theirs=[115..200], then intersection is 115 and move mine 301 | // but not theirs as theirs may collide with the next range 302 | // in thisIter. 303 | // move both iterators to next ranges 304 | if (mine.first > theirs.first) { 305 | j++; 306 | } 307 | else if (theirs.first > mine.first) { 308 | i++; 309 | } 310 | } 311 | } 312 | 313 | return std::move(result); 314 | } 315 | 316 | static interval_set complement(interval_set const& set, interval_set const& vocabulary) 317 | { 318 | interval_set result(vocabulary); 319 | if (result.empty()) 320 | { 321 | return std::move(result); 322 | } 323 | 324 | return std::move(interval_set::subtract(result, set)); 325 | } 326 | 327 | static interval_set complement(interval_set const& set, interval_type vocabulary) 328 | { 329 | return std::move(complement(set, interval_set::of(vocabulary))); 330 | } 331 | 332 | static interval_set subtract(interval_set const& left, interval_set const& right) 333 | { 334 | if (left.empty()) 335 | { 336 | return left; 337 | } 338 | 339 | if (right.empty()) 340 | { 341 | return left; 342 | } 343 | 344 | interval_set result(left); 345 | size_t result_index = 0; 346 | size_t right_index = 0; 347 | while (result_index < result.pairs().size() && right_index < right.pairs().size()) 348 | { 349 | interval_type result_interval = result.pairs()[result_index]; 350 | interval_type right_interval = right.pairs()[right_index]; 351 | 352 | // operation: (result_interval - right_interval) and update indexes 353 | 354 | if (right_interval.second <= result_interval.first) 355 | { 356 | right_index++; 357 | continue; 358 | } 359 | 360 | if (right_interval.first >= result_interval.second) 361 | { 362 | result_index++; 363 | continue; 364 | } 365 | 366 | interval_type before_current; 367 | interval_type after_current; 368 | if (right_interval.first > result_interval.first) 369 | { 370 | before_current = interval_type(result_interval.first, right_interval.first); 371 | } 372 | 373 | if (right_interval.second < result_interval.second) 374 | { 375 | after_current = interval_type(right_interval.second, result_interval.second); 376 | } 377 | 378 | if (before_current.second > before_current.first) 379 | { 380 | if (after_current.second > after_current.first) 381 | { 382 | // split the current interval into two 383 | result._pairs[result_index] = before_current; 384 | result._pairs.insert(result._pairs.begin() + static_cast(result_index) + 1, after_current); 385 | result_index++; 386 | right_index++; 387 | continue; 388 | } 389 | else 390 | { 391 | // replace the current interval 392 | result._pairs[result_index] = before_current; 393 | result_index++; 394 | continue; 395 | } 396 | } 397 | else 398 | { 399 | if (after_current.second > after_current.first) 400 | { 401 | // replace the current interval 402 | result._pairs[result_index] = after_current; 403 | right_index++; 404 | continue; 405 | } 406 | else 407 | { 408 | // remove the current interval (thus no need to increment resultI) 409 | result._pairs.erase(result._pairs.begin() + static_cast(result_index)); 410 | continue; 411 | } 412 | } 413 | } 414 | 415 | // If right_index reached right.pairs().size(), no more intervals to subtract from result. 416 | // If result_index reached result.pairs().size(), we would be subtracting from an empty set. 417 | // Either way, we are done. 418 | return std::move(result); 419 | } 420 | }; 421 | 422 | template 423 | bool operator== (interval_set<_Ty, _AllocX> const& x, interval_set<_Ty, _AllocY> const& y) 424 | { 425 | return x.pairs() == y.pairs(); 426 | } 427 | 428 | template<> 429 | struct to_string> 430 | { 431 | std::wstring operator() (interval_set const& set, bool elements_are_characters) const 432 | { 433 | if (set.pairs().empty()) 434 | { 435 | return L"{}"; 436 | } 437 | 438 | std::wostringstream stream; 439 | if (set.size() > 1) 440 | { 441 | stream << L"{"; 442 | } 443 | 444 | bool first = true; 445 | for each (interval_set::interval_type const& interval in set.pairs()) 446 | { 447 | if (first) 448 | { 449 | first = false; 450 | } 451 | else 452 | { 453 | stream << L", "; 454 | } 455 | 456 | if (interval.first == (interval.second - 1)) 457 | { 458 | if (interval.first == token::eof) 459 | { 460 | stream << L""; 461 | } 462 | else if (elements_are_characters) 463 | { 464 | stream << L"'" << static_cast(interval.first) << L"'"; 465 | } 466 | else 467 | { 468 | stream << interval.first; 469 | } 470 | } 471 | else 472 | { 473 | if (elements_are_characters) 474 | { 475 | stream << L"'" << static_cast(interval.first) << L"'..'" << static_cast(interval.second - 1) << L"'"; 476 | } 477 | else 478 | { 479 | stream << interval.first << L".." << (interval.second - 1); 480 | } 481 | } 482 | } 483 | 484 | if (set.size() > 1) 485 | { 486 | stream << L"}"; 487 | } 488 | 489 | return stream.str(); 490 | } 491 | 492 | std::wstring operator() (interval_set const& set) const 493 | { 494 | return (*this)(set, false); 495 | } 496 | }; 497 | 498 | } 499 | } 500 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/murmur_hash.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | namespace antlr4 { 8 | namespace misc { 9 | 10 | struct murmur_hash 11 | { 12 | private: 13 | murmur_hash() = delete; 14 | murmur_hash(murmur_hash const&) = delete; 15 | murmur_hash& operator= (murmur_hash const&) = delete; 16 | 17 | private: 18 | static const int32_t default_seed = 0; 19 | 20 | public: 21 | static int32_t initialize(int32_t seed = default_seed) 22 | { 23 | return seed; 24 | } 25 | 26 | static int32_t update(int32_t hash, int32_t value) 27 | { 28 | const int32_t c1 = 0xCC9E2D51; 29 | const int32_t c2 = 0x1B873593; 30 | const int32_t r1 = 15; 31 | const int32_t r2 = 13; 32 | const int32_t m = 5; 33 | const int32_t n = 0xE6546B64; 34 | 35 | int32_t k = value; 36 | k = k * c1; 37 | k = (k << r1) | static_cast(static_cast(k) >> (32 - r1)); 38 | k = k * c2; 39 | 40 | hash = hash ^ k; 41 | hash = (hash << r2) | static_cast(static_cast(hash) >> (32 - r2)); 42 | hash = hash * m + n; 43 | 44 | return hash; 45 | } 46 | 47 | template> 48 | static int32_t update(int32_t hash, _T value, _Hasher const& hasher) 49 | { 50 | return update(hash, static_cast(hasher(value))); 51 | } 52 | 53 | template> 54 | static int32_t update(int32_t hash, _T value) 55 | { 56 | return update(hash, value, _Hasher()); 57 | } 58 | 59 | static int32_t finish(int32_t hash, size_t number_of_words) 60 | { 61 | hash = hash ^ static_cast(number_of_words * 4); 62 | hash = hash ^ static_cast(static_cast(hash) >> 16); 63 | hash = hash * static_cast(0x85EBCA6B); 64 | hash = hash ^ static_cast(static_cast(hash) >> 13); 65 | hash = hash * static_cast(0xC2B2AE35); 66 | hash = hash ^ static_cast(static_cast(hash) >> 15); 67 | return hash; 68 | } 69 | }; 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/param_type.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | namespace misc { 8 | 9 | template 10 | struct param_type 11 | { 12 | private: 13 | static const bool pass_by_value = std::is_pointer<_Ty>::value || std::is_arithmetic<_Ty>::value; 14 | 15 | public: 16 | typedef typename std::conditional::type type; 17 | }; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/ptr_equal_to.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include "param_type.hpp" 6 | 7 | namespace antlr4 { 8 | namespace misc { 9 | 10 | template 11 | struct ptr_equal_to : public std::binary_function<_Tptr, _Tptr, bool> 12 | { 13 | typedef typename param_type<_Tptr>::type param_type; 14 | 15 | bool operator() (param_type _Left, param_type _Right) const 16 | { 17 | if (!_Left) 18 | { 19 | return !_Right; 20 | } 21 | else if (!_Right) 22 | { 23 | return false; 24 | } 25 | 26 | return *_Left == *_Right; 27 | } 28 | }; 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/ptr_hash.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | namespace misc { 8 | 9 | template::type>> 10 | struct ptr_hash : public std::unary_function<_Tptr, size_t> 11 | { 12 | typedef typename param_type<_Tptr>::type param_type; 13 | 14 | size_t operator() (param_type value) const 15 | { 16 | if (!value) 17 | { 18 | return std::hash()(nullptr); 19 | } 20 | 21 | return _Hasher()(*value); 22 | } 23 | }; 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/to_string.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include "param_type.hpp" 5 | 6 | namespace antlr4 { 7 | namespace misc { 8 | 9 | template 10 | struct to_string; 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/unordered_ptr_map.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | #include "ptr_equal_to.hpp" 7 | #include "ptr_hash.hpp" 8 | 9 | namespace antlr4 { 10 | namespace misc { 11 | 12 | template::type>, typename _Alloc = std::allocator>> 13 | using unordered_ptr_map = std::unordered_map<_Tptr, _Ty, ptr_hash<_Tptr, _Hasher>, ptr_equal_to<_Tptr>>; 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/unordered_ptr_set.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | #include "ptr_equal_to.hpp" 7 | #include "ptr_hash.hpp" 8 | 9 | namespace antlr4 { 10 | namespace misc { 11 | 12 | template::type>, typename _Alloc = std::allocator<_Tptr>> 13 | using unordered_ptr_set = std::unordered_set<_Tptr, ptr_hash<_Tptr, _Hasher>, ptr_equal_to<_Tptr>>; 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/uuid.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | namespace antlr4 { 8 | namespace misc { 9 | 10 | class uuid 11 | { 12 | private: 13 | int32_t _a; 14 | int16_t _b; 15 | int16_t _c; 16 | uint8_t _d; 17 | uint8_t _e; 18 | uint8_t _f; 19 | uint8_t _g; 20 | uint8_t _h; 21 | uint8_t _i; 22 | uint8_t _j; 23 | uint8_t _k; 24 | 25 | friend struct std::hash; 26 | friend bool operator== (uuid const& x, uuid const& y); 27 | 28 | public: 29 | uuid() 30 | : _a() 31 | , _b() 32 | , _c() 33 | , _d() 34 | , _e() 35 | , _f() 36 | , _g() 37 | , _h() 38 | , _i() 39 | , _j() 40 | , _k() 41 | { 42 | } 43 | 44 | uuid(int32_t a, int16_t b, int16_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g, uint8_t h, uint8_t i, uint8_t j, uint8_t k) 45 | : _a(a) 46 | , _b(b) 47 | , _c(c) 48 | , _d(d) 49 | , _e(e) 50 | , _f(f) 51 | , _g(g) 52 | , _h(h) 53 | , _i(i) 54 | , _j(j) 55 | , _k(k) 56 | { 57 | } 58 | 59 | uuid(uint32_t a, uint16_t b, uint16_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g, uint8_t h, uint8_t i, uint8_t j, uint8_t k) 60 | : _a(static_cast(a)) 61 | , _b(static_cast(b)) 62 | , _c(static_cast(c)) 63 | , _d(d) 64 | , _e(e) 65 | , _f(f) 66 | , _g(g) 67 | , _h(h) 68 | , _i(i) 69 | , _j(j) 70 | , _k(k) 71 | { 72 | } 73 | }; 74 | 75 | bool operator== (uuid const& x, uuid const& y) 76 | { 77 | if (x._a != y._a) 78 | return false; 79 | if (x._b != y._b) 80 | return false; 81 | if (x._c != y._c) 82 | return false; 83 | if (x._d != y._d) 84 | return false; 85 | if (x._e != y._e) 86 | return false; 87 | if (x._f != y._f) 88 | return false; 89 | if (x._g != y._g) 90 | return false; 91 | if (x._h != y._h) 92 | return false; 93 | if (x._i != y._i) 94 | return false; 95 | if (x._j != y._j) 96 | return false; 97 | if (x._k != y._k) 98 | return false; 99 | 100 | return true; 101 | } 102 | 103 | } 104 | } 105 | 106 | namespace std { 107 | template<> 108 | struct hash 109 | { 110 | size_t operator() (antlr4::misc::uuid const& value) const 111 | { 112 | auto result = value._a 113 | ^ ((static_cast(value._b) << 16) | static_cast(static_cast(value._c))) 114 | ^ ((static_cast(value._f) << 24) | value._k); 115 | 116 | return static_cast(result); 117 | } 118 | }; 119 | } 120 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/misc/visitor.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr4 { 5 | namespace misc { 6 | 7 | template 8 | class visitor; 9 | 10 | template 11 | class visitor<_T> 12 | { 13 | protected: 14 | virtual ~visitor() { } 15 | 16 | public: 17 | virtual void visit(_T const& node) = 0; 18 | }; 19 | 20 | template 21 | class visitor<_T, _Types...> : public visitor<_T>, public visitor<_Types...> 22 | { 23 | public: 24 | using visitor<_Types...>::visit; 25 | using visitor<_T>::visit; 26 | }; 27 | 28 | template 29 | class visitable 30 | { 31 | protected: 32 | virtual ~visitable() { } 33 | 34 | public: 35 | virtual void accept(visitor<_Types...>& visitor) const = 0; 36 | }; 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/token.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | 6 | namespace antlr4 { 7 | 8 | class token 9 | { 10 | public: 11 | static const int32_t epsilon = -2; 12 | static const int32_t eof = -1; 13 | }; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include "parse_tree.hpp" 5 | #include "parse_tree_visitor.hpp" 6 | 7 | namespace antlr4 { 8 | namespace tree { 9 | 10 | std::wstring rule_node::text() const 11 | { 12 | throw std::runtime_error("not implemented"); 13 | } 14 | 15 | std::wstring rule_node::tree_text() const 16 | { 17 | throw std::runtime_error("not implemented"); 18 | } 19 | 20 | void rule_node::accept(parse_tree_visitor& visitor) const 21 | { 22 | visitor.visit_children(*this); 23 | } 24 | 25 | void terminal_node::accept(parse_tree_visitor& visitor) const 26 | { 27 | visitor.visit(*this); 28 | } 29 | 30 | void error_node::accept(parse_tree_visitor& visitor) const 31 | { 32 | visitor.visit(*this); 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include "../misc/visitor.hpp" 7 | 8 | namespace antlr4 { 9 | 10 | class token; 11 | 12 | namespace tree { 13 | 14 | class error_node; 15 | class terminal_node; 16 | class parse_tree_listener; 17 | class parse_tree_visitor; 18 | 19 | class parse_tree 20 | { 21 | protected: 22 | virtual ~parse_tree() { } 23 | 24 | public: 25 | enum class tree_type 26 | { 27 | rule, 28 | terminal, 29 | error, 30 | }; 31 | 32 | public: 33 | // used to avoid certain cases of dynamic_cast 34 | virtual tree_type type() const = 0; 35 | 36 | public: 37 | virtual std::shared_ptr parent() const = 0; 38 | virtual std::shared_ptr child(size_t index) const = 0; 39 | virtual size_t size() const = 0; 40 | 41 | virtual std::pair source_interval() const = 0; 42 | virtual std::wstring text() const = 0; 43 | virtual std::wstring tree_text() const = 0; 44 | 45 | virtual void accept(parse_tree_visitor& visitor) const = 0; 46 | }; 47 | 48 | class rule_node : public parse_tree 49 | { 50 | private: 51 | std::shared_ptr _parent; 52 | 53 | public: 54 | rule_node(std::shared_ptr const& parent) 55 | : _parent(parent) 56 | { 57 | } 58 | 59 | public: 60 | virtual void enter_rule(parse_tree_listener& /*listener*/) const { } 61 | virtual void exit_rule(parse_tree_listener& /*listener*/) const { } 62 | 63 | public: 64 | virtual tree_type type() const override final 65 | { 66 | return tree_type::rule; 67 | } 68 | 69 | public: 70 | virtual std::shared_ptr parent() const override 71 | { 72 | return _parent; 73 | } 74 | 75 | virtual std::shared_ptr child(size_t /*index*/) const override 76 | { 77 | return nullptr; 78 | } 79 | 80 | virtual size_t size() const override 81 | { 82 | return 0; 83 | } 84 | 85 | virtual std::pair source_interval() const override 86 | { 87 | return std::make_pair(~size_t(), ~0); 88 | } 89 | 90 | virtual std::wstring text() const override; 91 | virtual std::wstring tree_text() const override; 92 | 93 | virtual void accept(parse_tree_visitor& visitor) const override; 94 | }; 95 | 96 | class terminal_node : public parse_tree 97 | { 98 | private: 99 | const std::shared_ptr _token; 100 | const std::shared_ptr _parent; 101 | 102 | public: 103 | terminal_node(std::shared_ptr const& token, std::shared_ptr const& parent) 104 | : _token(token) 105 | , _parent(parent) 106 | { 107 | } 108 | 109 | public: 110 | std::shared_ptr const& token() const 111 | { 112 | return _token; 113 | } 114 | 115 | public: 116 | virtual tree_type type() const override 117 | { 118 | return tree_type::terminal; 119 | } 120 | 121 | public: 122 | virtual std::shared_ptr parent() const override 123 | { 124 | return _parent; 125 | } 126 | 127 | virtual std::shared_ptr child(size_t /*index*/) const override 128 | { 129 | return nullptr; 130 | } 131 | 132 | virtual size_t size() const override 133 | { 134 | return 0; 135 | } 136 | 137 | virtual std::pair source_interval() const override; 138 | virtual std::wstring text() const override; 139 | virtual std::wstring tree_text() const override; 140 | 141 | virtual void accept(parse_tree_visitor& visitor) const override; 142 | }; 143 | 144 | class error_node : public terminal_node 145 | { 146 | public: 147 | error_node(std::shared_ptr const& token, std::shared_ptr const& parent) 148 | : terminal_node(token, parent) 149 | { 150 | } 151 | 152 | public: 153 | virtual tree_type type() const override final 154 | { 155 | return tree_type::error; 156 | } 157 | 158 | public: 159 | virtual void accept(parse_tree_visitor& visitor) const override; 160 | }; 161 | 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree_listener.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr4 { 5 | namespace tree { 6 | 7 | class error_node; 8 | class parse_tree; 9 | class rule_node; 10 | class terminal_node; 11 | 12 | class parse_tree_listener 13 | { 14 | protected: 15 | virtual ~parse_tree_listener() { } 16 | 17 | public: 18 | virtual void enter_node(rule_node const& node) = 0; 19 | virtual void exit_node(rule_node const& node) = 0; 20 | virtual void visit(terminal_node const& node) = 0; 21 | virtual void visit(error_node const& node) = 0; 22 | }; 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree_visitor.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include "../misc/param_type.hpp" 5 | #include "../misc/visitor.hpp" 6 | 7 | namespace antlr4 { 8 | namespace tree { 9 | 10 | class error_node; 11 | class parse_tree; 12 | class rule_node; 13 | class terminal_node; 14 | 15 | class parse_tree_visitor : public misc::visitor 16 | { 17 | public: 18 | virtual void visit_children(rule_node const& node) = 0; 19 | }; 20 | 21 | template 22 | class parse_tree_visitor_impl : public parse_tree_visitor 23 | { 24 | public: 25 | typedef _TResult result_type; 26 | typedef typename misc::param_type<_TResult>::type result_param_type; 27 | 28 | private: 29 | parse_tree_visitor_impl(parse_tree_visitor_impl const&) = delete; 30 | parse_tree_visitor_impl& operator= (parse_tree_visitor_impl const&) = delete; 31 | 32 | private: 33 | _TResult _result; 34 | 35 | public: 36 | explicit parse_tree_visitor_impl() 37 | : _result() 38 | { 39 | } 40 | 41 | // This will not have the expected result because it's not managed as a stack. 42 | //_TResult result() const 43 | //{ 44 | // return _result; 45 | //} 46 | 47 | public: 48 | virtual void visit(parse_tree const& node) override; 49 | virtual void visit(terminal_node const& node) override; 50 | virtual void visit(error_node const& node) override; 51 | virtual void visit_children(rule_node const& node) override; 52 | 53 | protected: 54 | virtual _TResult default_result(); 55 | virtual _TResult aggregate_result(result_param_type aggregate, result_param_type next_result); 56 | virtual bool should_visit_next_child(rule_node const& node, result_param_type current_result); 57 | }; 58 | 59 | } 60 | } 61 | 62 | #include "parse_tree_visitor.inl" 63 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree_visitor.inl: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | #include "parse_tree_visitor.hpp" 5 | 6 | namespace antlr4 { 7 | namespace tree { 8 | 9 | template 10 | void parse_tree_visitor_impl<_TResult>::visit(parse_tree const& node) 11 | { 12 | node.accept(*this); 13 | } 14 | 15 | template 16 | void parse_tree_visitor_impl<_TResult>::visit(terminal_node const& /*node*/) 17 | { 18 | _result = default_result(); 19 | } 20 | 21 | template 22 | void parse_tree_visitor_impl<_TResult>::visit(error_node const& /*node*/) 23 | { 24 | _result = default_result(); 25 | } 26 | 27 | template 28 | void parse_tree_visitor_impl<_TResult>::visit_children(rule_node const& node) 29 | { 30 | _TResult result(default_result()); 31 | size_t size = node.size(); 32 | for (size_t i = 0; i < size; i++) 33 | { 34 | if (!should_visit_next_child(node, result)) 35 | { 36 | break; 37 | } 38 | 39 | std::shared_ptr child = node.child(i); 40 | child->accept(*this); 41 | result = aggregate_result(result, _result); 42 | } 43 | 44 | _result = result; 45 | } 46 | 47 | template 48 | _TResult parse_tree_visitor_impl<_TResult>::default_result() 49 | { 50 | return _TResult(); 51 | } 52 | 53 | template 54 | _TResult parse_tree_visitor_impl<_TResult>::aggregate_result(result_param_type /*aggregate*/, result_param_type next_result) 55 | { 56 | return next_result; 57 | } 58 | 59 | template 60 | bool parse_tree_visitor_impl<_TResult>::should_visit_next_child(rule_node const& /*node*/, result_param_type /*current_result*/) 61 | { 62 | return true; 63 | } 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree_walker.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | 6 | #include "parse_tree_walker.hpp" 7 | 8 | #include "parse_tree.hpp" 9 | #include "parse_tree_listener.hpp" 10 | 11 | #if defined(_MSC_VER) && (_MSC_VER == 1800) 12 | #undef assert 13 | #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) ) 14 | #endif 15 | 16 | namespace antlr4 { 17 | namespace tree { 18 | 19 | namespace { 20 | 21 | template 22 | inline Target polymorphic_downcast(Source* x) 23 | { 24 | assert(dynamic_cast(x) == static_cast(x)); 25 | return static_cast(x); 26 | } 27 | 28 | template 29 | inline Target polymorphic_downcast(Source& x) 30 | { 31 | assert(dynamic_cast::type*>(&x) == &static_cast(x)); 32 | return static_cast(x); 33 | } 34 | 35 | } 36 | 37 | parse_tree_walker parse_tree_walker::_instance; 38 | 39 | void parse_tree_walker::walk(parse_tree_listener& listener, parse_tree const& tree) 40 | { 41 | switch (tree.type()) 42 | { 43 | case parse_tree::tree_type::error: 44 | { 45 | error_node const& error = polymorphic_downcast(tree); 46 | listener.visit(error); 47 | return; 48 | } 49 | 50 | case parse_tree::tree_type::terminal: 51 | { 52 | terminal_node const& terminal = polymorphic_downcast(tree); 53 | listener.visit(terminal); 54 | return; 55 | } 56 | 57 | case parse_tree::tree_type::rule: 58 | { 59 | rule_node const& node = polymorphic_downcast(tree); 60 | enter_rule(listener, node); 61 | for (size_t i = 0; i < tree.size(); i++) 62 | { 63 | walk(listener, *tree.child(i)); 64 | } 65 | 66 | exit_rule(listener, node); 67 | return; 68 | } 69 | 70 | default: 71 | assert(!"Invalid context type."); 72 | return; 73 | } 74 | } 75 | 76 | void parse_tree_walker::enter_rule(parse_tree_listener& listener, rule_node const& tree) 77 | { 78 | listener.enter_node(tree); 79 | tree.enter_rule(listener); 80 | } 81 | 82 | void parse_tree_walker::exit_rule(parse_tree_listener& listener, rule_node const& tree) 83 | { 84 | tree.exit_rule(listener); 85 | listener.exit_node(tree); 86 | } 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /antlr4cpp/antlr/v4/runtime/tree/parse_tree_walker.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #pragma once 3 | 4 | namespace antlr4 { 5 | namespace tree { 6 | 7 | class error_node; 8 | class parse_tree; 9 | class parse_tree_listener; 10 | class rule_node; 11 | class terminal_node; 12 | 13 | class parse_tree_walker 14 | { 15 | private: 16 | static parse_tree_walker _instance; 17 | 18 | protected: 19 | parse_tree_walker() 20 | { 21 | } 22 | 23 | virtual ~parse_tree_walker() 24 | { 25 | } 26 | 27 | public: 28 | static parse_tree_walker& instance() 29 | { 30 | return _instance; 31 | } 32 | 33 | public: 34 | virtual void walk(parse_tree_listener& listener, parse_tree const& tree); 35 | 36 | protected: 37 | virtual void enter_rule(parse_tree_listener& listener, rule_node const& tree); 38 | virtual void exit_rule(parse_tree_listener& listener, rule_node const& tree); 39 | }; 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /antlr4cpp/antlr4cpp.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Terence Parr, Sam Harwell. Licensed under the BSD license. See LICENSE in the project root for license information. 2 | #include "stdafx.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int _tmain(int /*argc*/, _TCHAR* /*argv*/[]) 9 | { 10 | antlr::test::test_graph_nodes(); 11 | antlr::test::test_interval_set(); 12 | antlr::test::test_visitor_inheritance(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /antlr4cpp/antlr4cpp.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {2484DFD0-BDD6-48BC-9218-32D57DC7BA03} 15 | Win32Proj 16 | antlr4cpp 17 | 18 | 19 | 20 | Application 21 | true 22 | v140 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v140 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | Use 51 | EnableAllWarnings 52 | Disabled 53 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 54 | true 55 | . 56 | 57 | 58 | Console 59 | true 60 | 61 | 62 | 63 | 64 | EnableAllWarnings 65 | Use 66 | MaxSpeed 67 | true 68 | true 69 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 70 | true 71 | . 72 | 73 | 74 | Console 75 | true 76 | true 77 | true 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | Create 133 | Create 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /antlr4cpp/antlr4cpp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {cb60996a-ccdf-4893-8f34-03ff82540176} 18 | 19 | 20 | {cc1b482e-1d96-4138-81cc-e051b633066a} 21 | 22 | 23 | {257680ed-4e7d-4fc8-a6e7-6b1ef5ef5dd0} 24 | 25 | 26 | {548402ff-58c4-47ea-8ba1-2e58de634c7a} 27 | 28 | 29 | {e247666a-2bc2-49c2-9ed0-e2ed2e28f041} 30 | 31 | 32 | {8c7b2f71-48ad-4993-a92a-d48d315d2df1} 33 | 34 | 35 | {53bb9ecd-7354-4df9-bf89-6d25266a79af} 36 | 37 | 38 | {770a12b8-3caf-4da2-9522-b5f828fc4877} 39 | 40 | 41 | {b1cbcc9a-55fc-4bff-927f-44fbbbb70b94} 42 | 43 | 44 | {198d6a76-8266-4484-91ba-3d51a16ed6eb} 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Header Files 53 | 54 | 55 | Header Files 56 | 57 | 58 | Header Files\runtime\atn 59 | 60 | 61 | Header Files\runtime\misc 62 | 63 | 64 | Header Files\runtime\atn 65 | 66 | 67 | Header Files\test 68 | 69 | 70 | Header Files\runtime\atn 71 | 72 | 73 | Header Files\runtime\atn 74 | 75 | 76 | Header Files\runtime\atn 77 | 78 | 79 | Header Files\runtime\misc 80 | 81 | 82 | Header Files\runtime\misc 83 | 84 | 85 | Header Files\runtime\misc 86 | 87 | 88 | Header Files\runtime\misc 89 | 90 | 91 | Header Files\runtime\atn 92 | 93 | 94 | Header Files\runtime\atn 95 | 96 | 97 | Header Files\runtime\tree 98 | 99 | 100 | Header Files\runtime\tree 101 | 102 | 103 | Header Files\runtime\misc 104 | 105 | 106 | Header Files\runtime\misc 107 | 108 | 109 | Header Files\runtime\tree 110 | 111 | 112 | Header Files\runtime\tree 113 | 114 | 115 | Header Files\runtime\atn 116 | 117 | 118 | Header Files\runtime\atn 119 | 120 | 121 | Header Files\runtime\atn 122 | 123 | 124 | Header Files\runtime\dfa 125 | 126 | 127 | Header Files\runtime\misc 128 | 129 | 130 | Header Files\runtime 131 | 132 | 133 | Header Files\runtime\misc 134 | 135 | 136 | Header Files\runtime\misc 137 | 138 | 139 | Header Files\test 140 | 141 | 142 | Header Files\test 143 | 144 | 145 | 146 | 147 | Source Files 148 | 149 | 150 | Source Files 151 | 152 | 153 | Source Files\runtime\atn 154 | 155 | 156 | Source Files\runtime\atn 157 | 158 | 159 | Source Files\test 160 | 161 | 162 | Source Files\runtime\atn 163 | 164 | 165 | Source Files\runtime\atn 166 | 167 | 168 | Source Files\runtime\atn 169 | 170 | 171 | Source Files\runtime\tree 172 | 173 | 174 | Source Files\runtime\tree 175 | 176 | 177 | Source Files\runtime\atn 178 | 179 | 180 | Source Files\runtime\atn 181 | 182 | 183 | Source Files\test 184 | 185 | 186 | Source Files\test 187 | 188 | 189 | Source Files\runtime\atn 190 | 191 | 192 | 193 | 194 | Header Files\runtime\atn 195 | 196 | 197 | Header Files\runtime\tree 198 | 199 | 200 | -------------------------------------------------------------------------------- /antlr4cpp/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // antlr4cpp.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /antlr4cpp/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #pragma warning(disable: 4820) // 'bytes' bytes padding added after construct 'member_name' 9 | #pragma warning(disable: 4710) // 'function' : function not inlined 10 | #pragma warning(disable: 4512) // 'class' : assignment operator could not be generated 11 | #pragma warning(disable: 4625) // 'derived class' : copy constructor could not be generated because a base class copy constructor is inaccessible 12 | #pragma warning(disable: 4626) // 'derived class' : assignment operator could not be generated because a base class assignment operator is inaccessible 13 | #pragma warning(disable: 4350) // behavior change: 'member1' called instead of 'member2' (An rvalue cannot be bound to a non-const reference.) 14 | 15 | #if defined(_MSC_VER) && (_MSC_VER >= 1900) 16 | // new warnings in Visual Studio 2015 17 | #pragma warning(disable: 5025) // 'derived class': move assignment operator was implicitly defined as deleted 18 | #pragma warning(disable: 5026) // 'derived class': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted 19 | #pragma warning(disable: 5027) // 'derived class': move assignment operator was implicitly defined as deleted because a base class move assignment operator is inaccessible or deleted 20 | #endif 21 | 22 | #include "targetver.h" 23 | 24 | #include 25 | #include 26 | 27 | 28 | 29 | // TODO: reference additional headers your program requires here 30 | -------------------------------------------------------------------------------- /antlr4cpp/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.{build} 2 | os: Visual Studio 2015 RC 3 | init: 4 | - git config --global core.autocrlf true 5 | install: 6 | - set PATH=C:\Program Files (x86)\MSBuild\14.0\Bin;%PATH% 7 | configuration: 8 | - Debug 9 | platform: 10 | - Win32 11 | build: 12 | parallel: true 13 | verbosity: minimal 14 | --------------------------------------------------------------------------------