From 11fd57def249cbc6f4aee8dfaac5e7f346f483c0 Mon Sep 17 00:00:00 2001 From: dos-reis Date: Sat, 29 Nov 2014 19:21:28 +0000 Subject: Move code from bemol.cc to library. --- src/boot/bemol.cc | 60 ++++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 50 deletions(-) (limited to 'src/boot') diff --git a/src/boot/bemol.cc b/src/boot/bemol.cc index 34870e13..b36b174d 100644 --- a/src/boot/bemol.cc +++ b/src/boot/bemol.cc @@ -35,7 +35,7 @@ // --% Description: #include -#include +#include #include #include #include @@ -50,59 +50,11 @@ using namespace OpenAxiom; // -- Reading input source files -- // -// A physical line is just raw text, with coupled with location -// information such as line and indentation column. -struct Line : std::string { - LineNumber number; - ColumnIndex indent; - Line() : number(), indent() { } - - std::string sub_string(ColumnIndex s, ColumnIndex e) const { - return substr(s, e - s); - } -}; - -// A program fragment is a logical line, composed of possibly -// several physical lines subject to the off-side rule. As a -// special case, a line ending with the underbar character -// continues to the next next with disregard to the off-side rule. -struct Fragment : std::vector { - explicit operator bool() const { return not empty(); } - bool line_continuation() const { - return not empty() and back().back() == '_'; - } - ColumnIndex last_indent() const { - return empty() ? 0 : back().indent; - } - using std::vector::operator[]; - const Line& operator()(const OpenAxiom::FragmentCursor& pos) const { - return (*this)[pos.line]; - } - uint8_t operator[](const OpenAxiom::FragmentCursor& pos) const { - return (*this)[pos.line][pos.column]; - } - uint8_t advance(OpenAxiom::FragmentCursor& pos) const { - return (*this)[pos.line][pos.column++]; - } - - bool covering(const OpenAxiom::FragmentCursor& pos) const { - return pos.column < (*this)[pos.line].size(); - } -}; - -// Formatting program fragments. -static std::ostream& -operator<<(std::ostream& os, const Fragment& f) { - std::copy(f.begin(), f.end(), - std::ostream_iterator(std::cout, "\n")); - return os; -} - - // A source input transform a character stream into a program fragment // stream, delivering a fragment one at a time. struct SourceInput { SourceInput(std::istream& is) : input(is) { } + // Return the next program fragment from this input source. Fragment get(); private: @@ -235,6 +187,14 @@ using TokenSequence = OpenAxiom::TokenStream; // -- +namespace { + struct Parser { + TokenSequence* tokens; + }; +} + +// -- + static void translate_source_file(SourceInput& src, std::ostream& out, const char* path) { while (auto f = src.get()) { -- cgit v1.2.3