Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
// Copyright (C) 2013-2014, Gabriel Dos Reis.
2
// All rights reserved.
3
// Written by Gabriel Dos Reis.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
// - Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
//
12
// - Redistributions in binary form must reproduce the above copyright
13
// notice, this list of conditions and the following disclaimer in
14
// the documentation and/or other materials provided with the
15
// distribution.
16
//
17
// - Neither the name of OpenAxiom. nor the names of its contributors
18
// may be used to endorse or promote products derived from this
19
// software without specific prior written permission.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
#include <open-axiom/token>
34
#include <ostream>
35
#include <iostream>
36
37
namespace OpenAxiom {
38
std::ostream& operator<<(std::ostream& os, const Locus& l) {
39
return os << '{' << l.line << ", " << l.column << '}';
40
}
41
42
std::ostream&
43
operator<<(std::ostream& os, TokenCategory tc) {
44
switch (tc) {
45
case TokenCategory::Unclassified: os << "UNCLASSIFIED"; break;
46
case TokenCategory::Whitespace: os << "WHITESPACE"; break;
47
case TokenCategory::Comment: os << "COMMENT"; break;
48
case TokenCategory::Punctuator: os << "PUNCTUATOR"; break;
49
case TokenCategory::Operator: os << "OPERATOR"; break;
50
case TokenCategory::Integer: os << "INTEGER"; break;
51
case TokenCategory::FloatingPoint: os << "FLOATINGPOINT"; break;
52
case TokenCategory::String: os << "STRING"; break;
53
case TokenCategory::Keyword: os << "KEYWORD"; break;
54
case TokenCategory::Identifier: os << "IDENTIFIER"; break;
55
case TokenCategory::Formatting: os << "FORMATTING"; break;
56
case TokenCategory::Junk: os << "JUNK"; break;
57
default: os << "????"; break;
58
}
59
return os;
60
}
61
62
63
bool separator_or_punctuator(uint8_t c) {
64
switch (c) {
65
case '.': case '`': case '^': case '&': case '~': case '*':
66
case '-': case '+': case ';': case ',': case '@': case '|':
67
case '\'': case ':': case '=': case '\\': case '"': case '/':
68
case '(': case ')': case '{': case '}': case '[': case ']':
69
case '<': case '>': case '#': case ' ':
70
return true;
71
default:
72
return false;
73
}
74
}
75
76
namespace {
77
struct TokenMapEntry {
78
const char* const text;
79
const TokenCategory category;
80
const TokenValue value;
81
const Language dialect; // = Language::Spad
82
};
83
}
84
85
const TokenMapEntry token_map[] {
86
#undef OPENAXIOM_DEFINE_TOKEN
87
#define OPENAXIOM_DEFINE_TOKEN(T, N, C, ...) \
88
{ N, TokenCategory::C, TokenValue::T, __VA_ARGS__ },
89
#include <open-axiom/token-value>
90
#undef OPENAXIOM_DEFINE_TOKEN
91
};
92
93
TokenClassification
94
classify(const std::string& s) {
95
for (auto& t : token_map) {
96
if (t.text == s)
97
return { t.category, t.value };
98
}
99
return { TokenCategory::Identifier, TokenValue::Unknown };
100
}
101
102
std::ostream&
103
operator<<(std::ostream& os, TokenValue tv) {
104
if (tv < TokenValue::Artificial)
105
os << token_map[uint8_t(tv)].text;
106
else switch (tv) {
107
case TokenValue::Indent: os << "%INDENT"; break;
108
case TokenValue::Unindent: os << "%UNIDENT"; break;
109
case TokenValue::Justify: os << "%JUSTIFY"; break;
110
default: os << "%ALIEN"; break;
111
}
112
113
return os;
114
}
115
116
}
117
118