| Author | Dave Jarvis <email> |
|---|---|
| Date | 2015-02-28 10:53:59 GMT-0800 |
| Commit | 5edaac16f3640dabf464b918e9dacbe5b4a98ea0 |
| Parent | 98f90b6 |
| Delta | 72 lines added, 4 lines removed, 68-line increase |
|---|
| package com.whitemagicsoftware.rxm; | ||
| +import java.io.IOException; | ||
| import java.io.StringReader; | ||
| + | ||
| +import java.nio.charset.Charset; | ||
| +import java.nio.file.Files; | ||
| +import java.nio.file.Paths; | ||
| import java_cup.runtime.*; | ||
| public Parser( String rxm ) throws Exception { | ||
| Yylex lexer = new Yylex( new StringReader( rxm ) ); | ||
| - | ||
| parser p = new parser( lexer ); | ||
| } | ||
| catch(Throwable e) { | ||
| - System.err.println("At line " + String.valueOf(lexer.line_num()) + ", near \"" + lexer.buff() + "\" :"); | ||
| - System.err.println(" " + e.getMessage()); | ||
| + throw new ParserException( | ||
| + lexer.line_num(), lexer.buff(), e.getMessage() ); | ||
| } | ||
| } | ||
| -} | ||
| + | ||
| + /** | ||
| + * @param args Contains the file name with an <b>rxm</b> to parse. | ||
| + */ | ||
| + public static void main( String args[] ) throws Exception { | ||
| + Parser parser = new Parser( readFile( args[0] ) ); | ||
| + } | ||
| + /** | ||
| + * Reads the entire contents of the file (specified by the given path) | ||
| + * | ||
| + */ | ||
| + private static String readFile( String path ) throws IOException { | ||
| + Charset encoding = Charset.defaultCharset(); | ||
| + byte[] encoded = Files.readAllBytes( Paths.get(path) ); | ||
| + return new String(encoded, encoding); | ||
| + } | ||
| +} | ||
| +package com.whitemagicsoftware.rxm; | ||
| + | ||
| +/** | ||
| + * Indicates a problem happened while parsing. | ||
| + */ | ||
| +public class ParserException extends RuntimeException { | ||
| + /** The line number in the <b>rxm</b> that could not be parsed. */ | ||
| + private int lineNumber; | ||
| + | ||
| + /** The <b>rxm</b> context wherein the parsing failed. */ | ||
| + private String context; | ||
| + | ||
| + /** | ||
| + * Creates a new exception that indicates a problem parsing an <b>rxm</b> | ||
| + * string. | ||
| + * | ||
| + * @param lineNumber The problematic line in the <b>rxm</b>. | ||
| + * @param context The problematic code in the <b>rxm</b>. | ||
| + * @param message The problem with the <b>rxm</b>. | ||
| + */ | ||
| + public ParserException( int lineNumber, String context, String message ) { | ||
| + super( message ); | ||
| + setLineNumber( lineNumber ); | ||
| + setContext( context ); | ||
| + } | ||
| + | ||
| + private void setLineNumber( int lineNumber ) { | ||
| + this.lineNumber = lineNumber; | ||
| + } | ||
| + | ||
| + private void setContext( String context ) { | ||
| + this.context = context; | ||
| + } | ||
| + | ||
| + private int getLineNumber() { | ||
| + return this.lineNumber; | ||
| + } | ||
| + | ||
| + private String getContext() { | ||
| + return this.context; | ||
| + } | ||
| + | ||
| + public String toString() { | ||
| + return String.format( "Line: %s; near: \"%s\"; error: %s\n", | ||
| + getLineNumber(), getContext(), getMessage() ); | ||
| + } | ||
| +} | ||
| + | ||