2015-12-06 23:15:02 -05:00
using PerCederberg.Grammatica.Runtime ;
using System ;
2015-12-28 03:37:10 -05:00
namespace arookas {
2015-12-06 23:15:02 -05:00
// base exception type
2015-12-28 03:37:10 -05:00
public class sunCompilerException : Exception {
public sunCompilerException ( ) {
2015-12-06 23:15:02 -05:00
}
public sunCompilerException ( string format , params object [ ] args )
2015-12-28 14:17:09 -05:00
: base ( String . Format ( format , args ) ) { }
2015-12-06 23:15:02 -05:00
}
// exceptions that have a location in the source
2015-12-28 03:37:10 -05:00
public abstract class sunSourceException : sunCompilerException {
2015-12-06 23:15:02 -05:00
public abstract sunSourceLocation Location { get ; }
2015-12-28 03:37:10 -05:00
public sunSourceException ( ) {
2015-12-06 23:15:02 -05:00
}
2015-12-13 00:57:21 -05:00
public sunSourceException ( string format , params object [ ] args )
2015-12-28 14:17:09 -05:00
: base ( format , args ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
public class sunImportException : sunCompilerException {
2015-12-08 20:11:35 -05:00
public string Name { get ; private set ; }
public sunImportResult Result { get ; private set ; }
2015-12-28 03:37:10 -05:00
public override string Message {
get {
2015-12-13 01:20:26 -05:00
string format ;
2015-12-28 03:37:10 -05:00
switch ( Result ) {
case sunImportResult . Loaded : format = "Script '{0}' loaded successfully." ; break ; // Error: Success!
case sunImportResult . Skipped : format = "Script '{0}' was skipped." ; break ;
case sunImportResult . Missing : format = "Script '{0}' could not be found." ; break ;
case sunImportResult . FailedToLoad : format = "Script '{0}' failed to load." ; break ;
default : format = "Name: {0}, Result: {1}" ; break ;
2015-12-13 01:20:26 -05:00
}
return String . Format ( format , Name , Result ) ;
}
}
2015-12-08 20:11:35 -05:00
2015-12-28 03:37:10 -05:00
public sunImportException ( string name , sunImportResult result ) {
if ( name = = null ) {
2015-12-08 20:11:35 -05:00
throw new ArgumentNullException ( "name" ) ;
}
2015-12-28 03:37:10 -05:00
if ( ! result . IsDefined ( ) ) {
2015-12-08 20:11:35 -05:00
throw new ArgumentOutOfRangeException ( "name" ) ;
}
Name = name ;
Result = result ;
}
}
2015-12-06 23:15:02 -05:00
// wrapper around Grammatica exceptions
2015-12-28 03:37:10 -05:00
class sunParserException : sunSourceException {
2015-12-06 23:15:02 -05:00
string file ;
public ParseException Info { get ; private set ; }
public override string Message { get { return Info . ErrorMessage ; } }
public override sunSourceLocation Location { get { return new sunSourceLocation ( file , Info . Line , Info . Column ) ; } }
2015-12-28 03:37:10 -05:00
public sunParserException ( string file , ParseException info ) {
if ( file = = null ) {
2015-12-06 23:15:02 -05:00
throw new ArgumentNullException ( "file" ) ;
}
2015-12-28 03:37:10 -05:00
if ( info = = null ) {
2015-12-06 23:15:02 -05:00
throw new ArgumentNullException ( "info" ) ;
}
this . file = file ;
Info = info ;
}
}
// node exceptions
2015-12-28 03:37:10 -05:00
abstract class sunNodeException < TNode > : sunSourceException where TNode : sunNode {
2015-12-06 23:15:02 -05:00
public TNode Node { get ; private set ; }
public override sunSourceLocation Location { get { return Node . Location ; } }
2015-12-28 03:37:10 -05:00
protected sunNodeException ( TNode node ) {
if ( node = = null ) {
2015-12-06 23:15:02 -05:00
throw new ArgumentNullException ( "node" ) ;
}
Node = node ;
}
}
2015-12-28 03:37:10 -05:00
class sunRedeclaredBuiltinException : sunNodeException < sunBuiltinDeclaration > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Redeclared builtin '{0}'." , Node . Builtin . Value ) ; } }
public sunRedeclaredBuiltinException ( sunBuiltinDeclaration node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunUndefinedFunctionException : sunNodeException < sunFunctionCall > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Undefined function or builtin '{0}'." , Node . Function . Value ) ; } }
public sunUndefinedFunctionException ( sunFunctionCall node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunRedefinedFunctionException : sunNodeException < sunFunctionDefinition > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Redefined function '{0}'." , Node . Function . Value ) ; } }
public sunRedefinedFunctionException ( sunFunctionDefinition node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunUndeclaredVariableException : sunNodeException < sunIdentifier > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Undeclared variable '{0}'." , Node . Value ) ; } }
public sunUndeclaredVariableException ( sunIdentifier node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunRedeclaredVariableException : sunNodeException < sunIdentifier > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Redeclared variable '{0}'." , Node . Value ) ; } }
public sunRedeclaredVariableException ( sunIdentifier node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunAssignConstantException : sunNodeException < sunIdentifier > {
2015-12-27 21:56:52 -05:00
public override string Message { get { return String . Format ( "Constant '{0}' is read-only." , Node . Value ) ; } }
public sunAssignConstantException ( sunIdentifier node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-28 14:20:44 -05:00
}
class sunConstantExpressionException : sunNodeException < sunNode > {
public override string Message { get { return "Expression must be constant." ; } }
2015-12-27 21:56:52 -05:00
2015-12-28 14:20:44 -05:00
public sunConstantExpressionException ( sunNode node )
: base ( node ) { }
2015-12-27 21:56:52 -05:00
}
2015-12-28 03:37:10 -05:00
class sunRedeclaredParameterException : sunNodeException < sunIdentifier > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Redeclared parameter '{0}'." , Node . Value ) ; } }
public sunRedeclaredParameterException ( sunIdentifier node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunVariadicFunctionException : sunNodeException < sunFunctionDefinition > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Function '{0}' is defined as a variadic function (only builtins may be variadic)." , Node . Function . Value ) ; } }
public sunVariadicFunctionException ( sunFunctionDefinition node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunEscapeSequenceException : sunNodeException < sunStringLiteral > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Bad escape sequence in string." ) ; } }
public sunEscapeSequenceException ( sunStringLiteral node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunVariadicParameterListException : sunNodeException < sunParameterList > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Bad variadic parameter list." ) ; } }
public sunVariadicParameterListException ( sunParameterList node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunArgumentCountException : sunNodeException < sunFunctionCall > {
2015-12-21 04:49:22 -05:00
public sunCallableSymbol CalledSymbol { get ; private set ; }
2015-12-06 23:15:02 -05:00
public int ArgumentMinimum { get { return CalledSymbol . Parameters . Minimum ; } }
public int ArgumentCount { get { return Node . Arguments . Count ; } }
2015-12-28 03:37:10 -05:00
public override string Message {
get {
2015-12-06 23:15:02 -05:00
string format ;
2015-12-28 03:37:10 -05:00
if ( CalledSymbol . Parameters . IsVariadic ) {
2015-12-06 23:15:02 -05:00
// assuming to be missing because there's only a minimum
format = "Missing {0} argument(s) (expected at least {1}; got {2})." ;
}
2015-12-28 03:37:10 -05:00
else if ( Node . Arguments . Count < CalledSymbol . Parameters . Minimum ) {
2015-12-06 23:15:02 -05:00
format = "Missing {0} argument(s) (expected {1}; got {2})." ; // missing arguments
}
2015-12-28 03:37:10 -05:00
else {
2015-12-06 23:15:02 -05:00
format = "Too many arguments (expected {1}; got {2})." ; // extra arguments
}
return String . Format ( format , ArgumentMinimum - ArgumentCount , ArgumentMinimum , ArgumentCount ) ;
}
}
2015-12-21 04:49:22 -05:00
public sunArgumentCountException ( sunFunctionCall node , sunCallableSymbol calledSymbol )
2015-12-28 03:37:10 -05:00
: base ( node ) {
if ( calledSymbol = = null ) {
2015-12-06 23:15:02 -05:00
throw new ArgumentNullException ( "calledSymbol" ) ;
}
CalledSymbol = calledSymbol ;
}
}
2015-12-28 03:37:10 -05:00
class sunIdentifierException : sunNodeException < sunIdentifier > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Invalid identifier '{0}'." , Node . Value ) ; } }
public sunIdentifierException ( sunIdentifier node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunMissingImportException : sunNodeException < sunImport > {
2015-12-06 23:15:02 -05:00
public override string Message { get { return String . Format ( "Could not find import file '{0}'." , Node . ImportFile . Value ) ; } }
public sunMissingImportException ( sunImport node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunBreakException : sunNodeException < sunBreak > {
2015-12-28 14:19:14 -05:00
public override string Message { get { return "Misplaced break statement." ; } }
2015-12-06 23:15:02 -05:00
public sunBreakException ( sunBreak node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
2015-12-28 03:37:10 -05:00
class sunContinueException : sunNodeException < sunContinue > {
2015-12-28 14:19:14 -05:00
public override string Message { get { return "Misplaced continue statement." ; } }
2015-12-06 23:15:02 -05:00
public sunContinueException ( sunContinue node )
2015-12-28 14:17:09 -05:00
: base ( node ) { }
2015-12-06 23:15:02 -05:00
}
}