]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/RuntimeMetaData.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / RuntimeMetaData.cpp
1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2  * Use of this file is governed by the BSD 3-clause license that
3  * can be found in the LICENSE.txt file in the project root.
4  */
5
6 #include "RuntimeMetaData.h"
7
8 using namespace antlr4;
9
10 const std::string RuntimeMetaData::VERSION = "4.9.2";
11
12 std::string RuntimeMetaData::getRuntimeVersion() {
13   return VERSION;
14 }
15
16 void RuntimeMetaData::checkVersion(const std::string &generatingToolVersion, const std::string &compileTimeVersion) {
17   std::string runtimeVersion = VERSION;
18   bool runtimeConflictsWithGeneratingTool = false;
19   bool runtimeConflictsWithCompileTimeTool = false;
20
21   if (generatingToolVersion != "") {
22     runtimeConflictsWithGeneratingTool = runtimeVersion != generatingToolVersion
23       && getMajorMinorVersion(runtimeVersion) != getMajorMinorVersion(generatingToolVersion);
24   }
25
26   runtimeConflictsWithCompileTimeTool = runtimeVersion != compileTimeVersion
27     && getMajorMinorVersion(runtimeVersion) != getMajorMinorVersion(compileTimeVersion);
28
29   if (runtimeConflictsWithGeneratingTool) {
30     std::cerr << "ANTLR Tool version " << generatingToolVersion << " used for code generation does not match "
31       "the current runtime version " << runtimeVersion << std::endl;
32   }
33   if (runtimeConflictsWithCompileTimeTool) {
34     std::cerr << "ANTLR Runtime version " << compileTimeVersion << " used for parser compilation does not match "
35       "the current runtime version " << runtimeVersion << std::endl;
36   }
37 }
38
39 std::string RuntimeMetaData::getMajorMinorVersion(const std::string &version) {
40   size_t firstDot = version.find('.');
41   size_t secondDot = firstDot != std::string::npos ? version.find('.', firstDot + 1) : std::string::npos;
42   size_t firstDash = version.find('-');
43   size_t referenceLength = version.size();
44   if (secondDot != std::string::npos) {
45     referenceLength = std::min(referenceLength, secondDot);
46   }
47
48   if (firstDash != std::string::npos) {
49     referenceLength = std::min(referenceLength, firstDash);
50   }
51
52   return version.substr(0, referenceLength);
53 }