1
0
forked from Alepha/Alepha
Files
ADAM David Alan Martin 7410245314 Input stream stacking with line numbers!
This should help when building custom language parsers.  An input
stream can be augmented with this stackable streambuf to track
the current line number.  This can (and should) be done low in
the stack, so that any variable expansion and comment stripping
stages will not affect line number count.

The stage bolts on a stream state sidecar to point back to itself.
The observer for the current line peeks into the sidecar to see
the current line number tracking object for the stream and then
gets the current line number from that object.

The line number is the current line the input cursor is on.
Newline characters are treated as-if they're part of the current
line.  The newly created line will start on the first character
after the newline character.  This helps keep line-index counts
accurate too.  (Idea for a further stage?  Line index cursor
too?)
2024-04-03 08:29:10 -04:00

56 lines
1.7 KiB
C++

#include "../LineTrackingStreambuf.h"
#include <sstream>
#include <Alepha/Testing/test.h>
#include <Alepha/Utility/evaluation_helpers.h>
static auto init= Alepha::Utility::enroll <=[]
{
using namespace Alepha::Testing::exports;
"Do we see line numbers in a simple case?"_test <=[]( TestState &test )
{
const std::string c= "One Two Three\nFour\n\nFive\n\n\n\nSeven";
std::istringstream iss{ c };
iss >> Alepha::IOStreams::TrackLines{};
using Alepha::IOStreams::getLineNumber;
std::string s;
test.expect( getLineNumber( iss ) == 1, "Line should be 1." );
std::cerr << "Num: " << getLineNumber( iss ) << std::endl;
iss >> s; // One
std::cerr << "s: " << s << std::endl;
test.expect( getLineNumber( iss ) == 1, "Line should be 1." );
std::cerr << "Num: " << getLineNumber( iss ) << std::endl;
iss >> s; // Two
std::cerr << "s: " << s << std::endl;
test.expect( getLineNumber( iss ) == 1, "Line should be 1." );
std::cerr << "Num: " << getLineNumber( iss ) << std::endl;
iss >> s; // Three
std::cerr << "s: " << s << std::endl;
test.expect( getLineNumber( iss ) == 1, "Line should be 1." );
std::cerr << "Num: " << getLineNumber( iss ) << std::endl;
iss >> s; // Four
std::cerr << "s: " << s << std::endl;
test.expect( getLineNumber( iss ) == 2, "Line should be 2." );
std::cerr << "Num: " << getLineNumber( iss ) << std::endl;
iss >> s; // Five
std::cerr << "s: " << s << std::endl;
test.expect( getLineNumber( iss ) == 4, "Line should be 4." );
std::cerr << "Num: " << getLineNumber( iss ) << std::endl;
iss >> s; // Five
std::cerr << "s: " << s << std::endl;
test.expect( getLineNumber( iss ) == 8, "Line should be 8." );
};
};