Skip to content

Commit

Permalink
Fix error messages for multiple alias oddities.
Browse files Browse the repository at this point in the history
  • Loading branch information
k_john_gough_cp authored and k_john_gough_cp committed Aug 25, 2014
1 parent 126dd5b commit 466b8ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
11 changes: 7 additions & 4 deletions ParserGenerator/ErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ internal void AddWarning(int code, string msg, LexSpan spn)
/// <param name="spn">The span to which the error is attached</param>
/// <param name="num">The error number</param>
/// <param name="key">The featured string</param>
internal void ListError(LexSpan spn, int num, string key, char quote)
{ ListError(spn, num, key, quote, quote); }
internal void ListError( LexSpan spn, int num, string key, char quote ) {
string s = (quote == '\0' ? "" : quote.ToString());
ListError( spn, num, key, s, s );
}

void ListError(LexSpan spn, int num, string key, char lh, char rh)
void ListError(LexSpan spn, int num, string key, string lh, string rh)
{
string prefix, suffix, message;
if (spn == null)
Expand All @@ -116,8 +118,9 @@ void ListError(LexSpan spn, int num, string key, char lh, char rh)
case 70: prefix = "Invalid string escape"; suffix = ""; break;
case 82: prefix = "Character literal"; suffix = "exceeds maximum in imported token type"; break;
case 83: prefix = "Key"; suffix = "was not found in token alias list"; break;
case 103: prefix = "Highest char literal token"; suffix = "is very large"; break;
case 84: prefix = "Ambiguous alias"; suffix = "has multiple definitions "; break;

case 103: prefix = "Highest char literal token"; suffix = "is very large"; break;
default: prefix = "Error " + Convert.ToString(num, CultureInfo.InvariantCulture); suffix = "";
break;
}
Expand Down
13 changes: 8 additions & 5 deletions ParserGenerator/Grammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,21 @@ internal Terminal LookupOrDefineTerminal(Token token, string name, string alias,

private void CheckAndSetAlias( string alias, Terminal terminal, LexSpan span ) {
//
// Terminal not known in collection
// Check if this alias already known.
//
if (aliasTerms.ContainsKey( alias )) {
//
// It is an error if an alias denotes more than one logical token
// If an alias denotes more than one logical token then used
// occurrences of the literal string alias must be disallowed.
//
Terminal other = aliasTerms[alias];
if (other != terminal)
if (other != terminal) {
handler.AddWarning( 153,
String.Format( CultureInfo.InvariantCulture,
"Alias \"{0}\" also used for previous token: {1}",
alias, other.BaseString()), span );
"Alias \"{0}\" also used for previous token: {1}, used occurrences forbidden",
alias, other.BaseString() ), span );
aliasTerms[alias] = Terminal.Ambiguous;
}
}
else {
aliasTerms[alias] = terminal;
Expand Down
11 changes: 7 additions & 4 deletions ParserGenerator/ParseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,22 @@ private void AddSymbolsToProduction( Production prod, List<string> list ) {
Symbol symbol = null;

switch (TokenOf( str )) {
case Token.litchar:
case Token.litchar: // This is a character literal symbol
if (GPCG.ImportedTokens && Terminal.BumpsMax( str ))
handler.ListError( this.CurrentLocationSpan, 82, str, '\0' );
symbol = grammar.LookupTerminal( Token.litchar, str );
break;
case Token.litstring:
case Token.litstring: // This is a uned occurrence of a terminal alias.
String s = CharacterUtilities.CanonicalizeAlias( str );
if (!grammar.aliasTerms.ContainsKey( s ))
handler.ListError( this.CurrentLocationSpan, 83, str, '\0' );
else
else {
symbol = grammar.aliasTerms[s];
if (symbol == Terminal.Ambiguous) // Use of an ambiguous alias.
handler.ListError( this.CurrentLocationSpan, 84, str, '\0' );
}
break;
case Token.ident:
case Token.ident: // This is a used occurrence of a terminal name.
if (grammar.terminals.ContainsKey( str ))
symbol = grammar.terminals[str];
else
Expand Down
2 changes: 2 additions & 0 deletions ParserGenerator/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ internal Terminal(bool symbolic, string name)
}
}

internal static readonly Terminal Ambiguous = new Terminal( true, "$Ambiguous$" );

internal Terminal(bool symbolic, string name, string alias)
: this(symbolic, name)
{
Expand Down

0 comments on commit 466b8ba

Please sign in to comment.