// -*- C++ -*-1// Copyright (C) 2014-2015, 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 without6// modification, are permitted provided that the following conditions are7// met:8//9// - Redistributions of source code must retain the above copyright10// notice, this list of conditions and the following disclaimer.11//12// - Redistributions in binary form must reproduce the above copyright13// notice, this list of conditions and the following disclaimer in14// the documentation and/or other materials provided with the15// distribution.16//17// - Neither the name of OpenAxiom. nor the names of its contributors18// may be used to endorse or promote products derived from this19// software without specific prior written permission.20//21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER25// 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, OR28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3233// --% Author: Gabriel Dos Reis34// --% Description:3536#include <open-axiom/diagnostics>37#include <open-axiom/InputFragment>38#include <iostream>39#include <fstream>40#include <vector>41#include <string>42#include <stack>43#include <iterator>44#include <ctype.h>45#include <open-axiom/SourceInput>4647using namespace OpenAxiom;4849//50// -- Decomposing source files into lexical units of information --51//5253static std::ostream&54operator<<(std::ostream& os, const Token& t) {55os << t.category << '{' << t.start << '-' << t.end << '}';56return os;57}5859using TokenSequence = OpenAxiom::TokenStream<Token>;6061// --6263namespace {64struct Parser {65TokenSequence* tokens;66};67}6869// --7071static void72translate_source_file(SourceInput& src, std::ostream& out, const char* path) {73while (auto f = src.get()) {74out << "================================================\n";75out << f;76try {77TokenSequence ts { f, OpenAxiom::Language::Boot };78for (auto& t : ts) {79out << '\t' << t;80switch (t.category) {81case TokenCategory::Junk:82case TokenCategory::Unclassified:83out //<< f[t.start.line].sub_string(t.start.column, t.end.column)84<< " in file " << path85<< " at line " << t.start.line86<< ", column " << t.start.column;87break;88default:89break;90}91out << '\n';92}93}94catch(const OpenAxiom::EndOfStringUnseen& e) {95std::cerr << path << ": syntax error: "96<< "premature end of line before matching quote "97<< "of string literal on line " << e.line98<< " at column " << e.column99<< std::endl;100}101catch (const OpenAxiom::MissingExponent& e) {102std::cerr << path << ": syntax error: "103<< "missing exponent of floating point constant "104<< "on line " << e.line105<< ", column " << e.column106<< std::endl;107}108out << "================================================\n";109}110out << std::flush;111}112113static void114process_file(const char* path) {115std::ifstream in { path };116if (!in) {117std::cerr << "error: could not open file `" << path << "'"118<< std::endl;119return;120}121SourceInput src { in };122translate_source_file(src, std::cout, path);123}124125int main(int argc, char* argv[]) {126for (int i = 1; i < argc; ++i) {127process_file(argv[i]);128}129}130131132