Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
// -*- C++ -*-
2
// Copyright (C) 2014-2015, Gabriel Dos Reis.
3
// All rights reserved.
4
// Written by Gabriel Dos Reis.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are
8
// met:
9
//
10
// - Redistributions of source code must retain the above copyright
11
// notice, this list of conditions and the following disclaimer.
12
//
13
// - Redistributions in binary form must reproduce the above copyright
14
// notice, this list of conditions and the following disclaimer in
15
// the documentation and/or other materials provided with the
16
// distribution.
17
//
18
// - Neither the name of OpenAxiom. nor the names of its contributors
19
// may be used to endorse or promote products derived from this
20
// software without specific prior written permission.
21
//
22
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34
// --% Author: Gabriel Dos Reis
35
// --% Description:
36
37
#include <open-axiom/diagnostics>
38
#include <open-axiom/InputFragment>
39
#include <iostream>
40
#include <fstream>
41
#include <vector>
42
#include <string>
43
#include <stack>
44
#include <iterator>
45
#include <ctype.h>
46
#include <open-axiom/SourceInput>
47
48
using namespace OpenAxiom;
49
50
//
51
// -- Decomposing source files into lexical units of information --
52
//
53
54
static std::ostream&
55
operator<<(std::ostream& os, const Token& t) {
56
os << t.category << '{' << t.start << '-' << t.end << '}';
57
return os;
58
}
59
60
using TokenSequence = OpenAxiom::TokenStream<Token>;
61
62
// --
63
64
namespace {
65
struct Parser {
66
TokenSequence* tokens;
67
};
68
}
69
70
// --
71
72
static void
73
translate_source_file(SourceInput& src, std::ostream& out, const char* path) {
74
while (auto f = src.get()) {
75
out << "================================================\n";
76
out << f;
77
try {
78
TokenSequence ts { f, OpenAxiom::Language::Boot };
79
for (auto& t : ts) {
80
out << '\t' << t;
81
switch (t.category) {
82
case TokenCategory::Junk:
83
case TokenCategory::Unclassified:
84
out //<< f[t.start.line].sub_string(t.start.column, t.end.column)
85
<< " in file " << path
86
<< " at line " << t.start.line
87
<< ", column " << t.start.column;
88
break;
89
default:
90
break;
91
}
92
out << '\n';
93
}
94
}
95
catch(const OpenAxiom::EndOfStringUnseen& e) {
96
std::cerr << path << ": syntax error: "
97
<< "premature end of line before matching quote "
98
<< "of string literal on line " << e.line
99
<< " at column " << e.column
100
<< std::endl;
101
}
102
catch (const OpenAxiom::MissingExponent& e) {
103
std::cerr << path << ": syntax error: "
104
<< "missing exponent of floating point constant "
105
<< "on line " << e.line
106
<< ", column " << e.column
107
<< std::endl;
108
}
109
out << "================================================\n";
110
}
111
out << std::flush;
112
}
113
114
static void
115
process_file(const char* path) {
116
std::ifstream in { path };
117
if (!in) {
118
std::cerr << "error: could not open file `" << path << "'"
119
<< std::endl;
120
return;
121
}
122
SourceInput src { in };
123
translate_source_file(src, std::cout, path);
124
}
125
126
int main(int argc, char* argv[]) {
127
for (int i = 1; i < argc; ++i) {
128
process_file(argv[i]);
129
}
130
}
131
132