summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2009-02-15 01:18:01 +0000
committerAdrian Thurston <thurston@complang.org>2009-02-15 01:18:01 +0000
commite52e24db80a4b9b70e2d034a2eca5da217ca29c7 (patch)
tree3522708e182115f18d2146f4b995d48ae4ed8636
parent56c2836d55f950c5f010b3c4bf85a7bbba5c18a8 (diff)
downloadcolm-e52e24db80a4b9b70e2d034a2eca5da217ca29c7.tar.gz
Parse errors in patterns and constructors now report the line number in the
space of the colm source file.
-rw-r--r--colm/fsmrun.cpp30
-rw-r--r--colm/fsmrun.h2
-rw-r--r--colm/input.h27
-rw-r--r--colm/lmparse.kl12
-rw-r--r--colm/lmscan.rl5
-rw-r--r--colm/parsedata.cpp10
-rw-r--r--colm/parsetree.h22
-rw-r--r--colm/pdarun.cpp2
8 files changed, 69 insertions, 41 deletions
diff --git a/colm/fsmrun.cpp b/colm/fsmrun.cpp
index 8b0e205f..b027e028 100644
--- a/colm/fsmrun.cpp
+++ b/colm/fsmrun.cpp
@@ -45,9 +45,7 @@ void operator<<( ostream &out, exit_object & )
FsmRun::FsmRun( Program *prg ) :
prg(prg),
tables(prg->rtd->fsmTables),
- parser(0),
- line(1),
- position(0)
+ parser(0)
{
}
@@ -119,23 +117,27 @@ void FsmRun::streamPush( const char *data, long length )
/* Keep the position up to date after consuming text. */
void update_position( FsmRun *fsmRun, const char *data, long length )
{
- for ( int i = 0; i < length; i++ ) {
- if ( data[i] == '\n' )
- fsmRun->line += 1;
+ if ( !fsmRun->inputStream->handlesLine ) {
+ for ( int i = 0; i < length; i++ ) {
+ if ( data[i] == '\n' )
+ fsmRun->inputStream->line += 1;
+ }
}
- fsmRun->position += length;
+ fsmRun->inputStream->position += length;
}
/* Keep the position up to date after sending back text. */
void undo_position( FsmRun *fsmRun, const char *data, long length )
{
- for ( int i = 0; i < length; i++ ) {
- if ( data[i] == '\n' )
- fsmRun->line -= 1;
+ if ( !fsmRun->inputStream->handlesLine ) {
+ for ( int i = 0; i < length; i++ ) {
+ if ( data[i] == '\n' )
+ fsmRun->inputStream->line -= 1;
+ }
}
- fsmRun->position -= length;
+ fsmRun->inputStream->position -= length;
}
/* Should only be sending back whole tokens/ignores, therefore the send back
@@ -665,7 +667,7 @@ void FsmRun::sendIgnore( long id )
parser->ignore( tree );
/* Prepare for more scanning. */
- position += length;
+ inputStream->position += length;
region = parser->getNextRegion();
cs = tables->entryByRegion[region];
@@ -769,7 +771,7 @@ void FsmRun::attachInputStream( InputStream *in )
p = pe = runBuf->buf;
peof = 0;
eofSent = false;
- position = 0;
+ inputStream->position = 0;
}
long PdaRun::run()
@@ -937,7 +939,7 @@ long FsmRun::run( PdaRun *destParser )
}
/* Machine failed before finding a token. */
- cerr << "SCANNER ERROR" << endp;
+ cerr << "error:" << inputStream->line << ": scanner error" << endp;
}
space = runBuf->buf + FSM_BUFSIZE - pe;
diff --git a/colm/fsmrun.h b/colm/fsmrun.h
index af1edc10..7d96112c 100644
--- a/colm/fsmrun.h
+++ b/colm/fsmrun.h
@@ -121,8 +121,6 @@ struct FsmRun
bool eofSent;
RunBuf *runBuf;
bool gotoResume;
- long line;
- long position;
char *mark_enter[32];
char *mark_leave[32];
};
diff --git a/colm/input.h b/colm/input.h
index 3796c554..41feef25 100644
--- a/colm/input.h
+++ b/colm/input.h
@@ -33,6 +33,11 @@ struct RunBuf;
struct InputStream
{
+ InputStream( bool handlesLine ) :
+ line(1),
+ position(0),
+ handlesLine(handlesLine) {}
+
virtual ~InputStream() {}
/* Basic functions. */
@@ -51,12 +56,20 @@ struct InputStream
{ assert( false ); return 0; }
virtual void pushBackNamed()
{ assert( false ); }
+
+ long line;
+ long position;
+
+ /* This is set true for input streams that do their own line counting.
+ * Causes FsmRun to ignore NLs. */
+ bool handlesLine;
};
struct InputStreamString : public InputStream
{
- InputStreamString( const String &data )
- : data(data), offset(0), eof(false) {}
+ InputStreamString( const String &data ) :
+ InputStream(false),
+ data(data), offset(0), eof(false) {}
int getData( char *dest, int length );
int isEOF() { return eof; }
@@ -70,8 +83,9 @@ struct InputStreamString : public InputStream
struct InputStreamFile : public InputStream
{
- InputStreamFile( FILE *file )
- : file(file), queue(0) {}
+ InputStreamFile( FILE *file ) :
+ InputStream(false),
+ file(file), queue(0) {}
int getData( char *dest, int length );
int isEOF();
@@ -85,8 +99,9 @@ struct InputStreamFile : public InputStream
struct InputStreamFD : public InputStream
{
- InputStreamFD( long fd )
- : fd(fd), eof(false), queue(0) {}
+ InputStreamFD( long fd ) :
+ InputStream(false),
+ fd(fd), eof(false), queue(0) {}
int isEOF();
int needFlush();
diff --git a/colm/lmparse.kl b/colm/lmparse.kl
index ef34d20f..dc81e9d9 100644
--- a/colm/lmparse.kl
+++ b/colm/lmparse.kl
@@ -399,7 +399,7 @@ litpat_el_list: ;
litpat_el: TK_LitPat
final {
- PatternItem *patternItem = new PatternItem( $1->data,
+ PatternItem *patternItem = new PatternItem( $1->loc, $1->data,
PatternItem::InputText );
patternItemList->append( patternItem );
};
@@ -444,7 +444,7 @@ pattern_el_type_or_lit: region_qual TK_Word opt_repeat
final {
PdaFactor *factor = new PdaFactor( $2->loc, false, $1->nspaceQual,
$2->data, 0, $3->opt, $3->repeat );
- $$->patternItem = new PatternItem( factor, PatternItem::FactorType );
+ $$->patternItem = new PatternItem( $2->loc, factor, PatternItem::FactorType );
patternItemList->append( $$->patternItem );
};
@@ -453,7 +453,7 @@ pattern_el_type_or_lit: region_qual TK_Literal opt_repeat
PdaLiteral *literal = new PdaLiteral( $2->loc, *$2 );
PdaFactor *factor = new PdaFactor( $2->loc, false, $1->nspaceQual,
literal, 0, $3->opt, $3->repeat );
- $$->patternItem = new PatternItem( factor, PatternItem::FactorType );
+ $$->patternItem = new PatternItem( $2->loc, factor, PatternItem::FactorType );
patternItemList->append( $$->patternItem );
};
@@ -488,7 +488,7 @@ litrepl_el_list: ;
litrepl_el: TK_LitPat
final {
- ReplItem *replItem = new ReplItem( ReplItem::InputText, $1->data );
+ ReplItem *replItem = new ReplItem( $1->loc, ReplItem::InputText, $1->data );
replItemList->append( replItem );
};
@@ -499,7 +499,7 @@ repl_el_list: ;
repl_el: var_ref
final {
- ReplItem *replItem = new ReplItem( ReplItem::VarRefType, $1->varRef );
+ ReplItem *replItem = new ReplItem( $1->varRef->loc, ReplItem::VarRefType, $1->varRef );
replItemList->append( replItem );
};
@@ -508,7 +508,7 @@ repl_el: region_qual TK_Literal
PdaLiteral *literal = new PdaLiteral( $2->loc, *$2 );
PdaFactor *factor = new PdaFactor( $2->loc, false, $1->nspaceQual,
literal, 0, false, false );
- ReplItem *replItem = new ReplItem( ReplItem::FactorType, factor );
+ ReplItem *replItem = new ReplItem( $2->loc, ReplItem::FactorType, factor );
replItemList->append( replItem );
};
diff --git a/colm/lmscan.rl b/colm/lmscan.rl
index d772a887..83283c35 100644
--- a/colm/lmscan.rl
+++ b/colm/lmscan.rl
@@ -165,6 +165,9 @@ void Scanner::token( int type )
loc.line = line;
loc.col = column;
+ if ( tokdata != 0 && tokdata[toklen-1] == '\n' )
+ loc.line -= 1;
+
parser->token( loc, type, tokdata, toklen );
}
@@ -442,8 +445,6 @@ void Scanner::endSection( )
# Whitespace other than newline.
[ \t\r]+ => { updateCol(); };
-
- # If we are in a single line machine then newline may end the spec.
NL => { updateCol(); };
# Consume eof.
diff --git a/colm/parsedata.cpp b/colm/parsedata.cpp
index fd38e64e..2effebcf 100644
--- a/colm/parsedata.cpp
+++ b/colm/parsedata.cpp
@@ -1386,6 +1386,7 @@ void ParseData::initEmptyScanners()
InputStreamPattern::InputStreamPattern( Pattern *pattern )
:
+ InputStream(true),
pattern(pattern),
patItem(pattern->list->head),
offset(0),
@@ -1408,6 +1409,7 @@ KlangEl *InputStreamPattern::getLangEl( long &bindId, char *&data, long &length
bindId = patItem->bindId;
data = 0;
length = 0;
+ line = patItem->loc.line;
patItem = patItem->next;
offset = 0;
@@ -1418,6 +1420,9 @@ KlangEl *InputStreamPattern::getLangEl( long &bindId, char *&data, long &length
int InputStreamPattern::getData( char *dest, int length )
{
+ if ( offset == 0 )
+ line = patItem->loc.line;
+
assert ( patItem->type == PatternItem::InputText );
int available = patItem->data.length() - offset;
@@ -1491,6 +1496,7 @@ void InputStreamPattern::pushBackNamed()
InputStreamRepl::InputStreamRepl( Replacement *replacement )
:
+ InputStream(true),
replacement(replacement),
replItem(replacement->list->head),
offset(0),
@@ -1517,6 +1523,7 @@ KlangEl *InputStreamRepl::getLangEl( long &bindId, char *&data, long &length )
data = 0;
length = 0;
+ line = replItem->loc.line;
if ( replItem->type == ReplItem::FactorType ) {
if ( replItem->factor->literal != 0 ) {
@@ -1538,6 +1545,9 @@ KlangEl *InputStreamRepl::getLangEl( long &bindId, char *&data, long &length )
int InputStreamRepl::getData( char *dest, int length )
{
+ if ( offset == 0 )
+ line = replItem->loc.line;
+
assert ( replItem->type == ReplItem::InputText );
int available = replItem->data.length() - offset;
diff --git a/colm/parsetree.h b/colm/parsetree.h
index 96449c43..a9e4f6c1 100644
--- a/colm/parsetree.h
+++ b/colm/parsetree.h
@@ -907,14 +907,15 @@ struct PatternItem
InputText
};
- PatternItem( const String &data, Type type ) :
- factor(0), data(data), type(type), region(0),
+ PatternItem( const InputLoc &loc, const String &data, Type type ) :
+ loc(loc), factor(0), data(data), type(type), region(0),
varRef(0), bindId(0) {}
- PatternItem( PdaFactor *factor, Type type ) :
- factor(factor), type(type), region(0),
+ PatternItem( const InputLoc &loc, PdaFactor *factor, Type type ) :
+ loc(loc), factor(factor), type(type), region(0),
varRef(0), bindId(0) {}
+ InputLoc loc;
PdaFactor *factor;
String data;
Type type;
@@ -935,15 +936,16 @@ struct ReplItem
FactorType
};
- ReplItem( Type type, const String &data ) :
- type(type), data(data), varRef(0), bindId(0) {}
+ ReplItem( const InputLoc &loc, Type type, const String &data ) :
+ loc(loc), type(type), data(data), varRef(0), bindId(0) {}
- ReplItem( Type type, LangVarRef *varRef ) :
- type(type), varRef(varRef), bindId(0) {}
+ ReplItem( const InputLoc &loc, Type type, LangVarRef *varRef ) :
+ loc(loc), type(type), varRef(varRef), bindId(0) {}
- ReplItem( Type type, PdaFactor *factor ) :
- type(type), factor(factor), bindId(0) {}
+ ReplItem( const InputLoc &loc, Type type, PdaFactor *factor ) :
+ loc(loc), type(type), factor(factor), bindId(0) {}
+ InputLoc loc;
Type type;
String data;
LangVarRef *varRef;
diff --git a/colm/pdarun.cpp b/colm/pdarun.cpp
index d3a3fd87..07bd8d09 100644
--- a/colm/pdarun.cpp
+++ b/colm/pdarun.cpp
@@ -701,7 +701,7 @@ _out:
ostream &PdaRun::parse_error( int tokId, Tree *tree )
{
- cerr << "error:" << fsmRun->line << ": at token ";
+ cerr << "error:" << fsmRun->inputStream->line << ": at token ";
if ( tokId < 128 )
cerr << "\"" << tables->rtd->lelInfo[tokId].name << "\"";
else