Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages for invalid delimiters (#82) #84

Merged
merged 1 commit into from
Mar 14, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Modyllic/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function partial($schema, $sql, $filename="SQL", $delim=null) {
function next($whitespace=false) {
$next = $this->tok->next($whitespace);
if ( $next instanceOf Modyllic_Token_Error ) {
throw $this->error( "Syntax error" );
throw $this->error( $next->value() );
}
return $next;
}
Expand Down
24 changes: 18 additions & 6 deletions Modyllic/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,23 @@ class Modyllic_Token_EOF extends Modyllic_Token_EOC {}
* one it will always be returned by next().
*/
class Modyllic_Token_Error extends Modyllic_Token_Except {
private $row;
private $col;
protected $row;
protected $col;
function __construct($pos, $row,$col) {
$this->pos = $pos;
$this->row = $row;
$this->col = $col;
}
function value() {
return "Syntax Error at ".$this->row.", ".$this->col;
return "Syntax error";
}
}

class Modyllic_Token_Delimiter_Error extends Modyllic_Token_Error {
function value() {
return "Invalid delimiter declaration";
}
}

class Modyllic_Token_SOC extends Modyllic_Token_EOC {}

Expand Down Expand Up @@ -374,7 +379,7 @@ function is_delimiter() {
return isset($this->delimiter) and preg_match( "/\G\Q$this->delimiter\E/", $this->cmdstr, $matches, 0, $this->pos );
}
function is_new_delimiter(&$matches) {
return $this->prev instanceOf Modyllic_Token_SOC and preg_match( "/\G(DELIMITER\s+(\S+)\s*(?:\n|\z))/i", $this->cmdstr, $matches, 0, $this->pos);
return $this->prev instanceOf Modyllic_Token_SOC and preg_match( "/\G(DELIMITER\s+(\S+)([^\n]*?)(?=\n|\z))/i", $this->cmdstr, $matches, 0, $this->pos);
}
function is_string() {
return isset( $this->quote_chars[$this->cmdstr[$this->pos]] );
Expand Down Expand Up @@ -537,9 +542,16 @@ function next( $whitespace = FALSE, $peek = FALSE ) {
$this->cur = new Modyllic_Token_Whitespace( $this->pos, $matches[1] );
}
else if ( $this->is_new_delimiter($matches) ) {
$this->pos += strlen($matches[1]);
$this->delimiter = $matches[2];
$this->cur = new Modyllic_Token_NewDelim( $this->pos, $matches[1]);
$this->pos += strlen($matches[1]);
if ( preg_match("/\S/",$matches[3], $offset, PREG_OFFSET_CAPTURE) ) {
$this->pos -= strlen($matches[3]);
$this->pos += $offset[0][1];
$this->cur = new Modyllic_Token_Delimiter_Error($this->pos, $this->line(), $this->col() );
}
else {
$this->cur = new Modyllic_Token_NewDelim( $this->pos, $matches[1]);
}
}
else if ( $this->is_reserved($matches) ) {
$this->pos += strlen($matches[1]);
Expand Down
14 changes: 14 additions & 0 deletions test/General_Functional.t
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ $table = $schema->tables['test'];
$index = array_pop($table->indexes);
is( $index->name, "id", "Generated index name is correct");
is( $index->dynamic_name, true, "Generated index name is flagged as dynamic");

$msg = "Trailing words on DELIMITERs produce reasonable error messages";
try {
$sql = 'DELIMITER ; |';
$schema = $parser->parse($sql);
fail($msg);
}
catch (Modyllic_Exception $e) {
if ( ! ok( ! preg_match("/Modyllic_Token/",$e->getMessage()), $msg ) ) {
foreach (explode("\n",$e->getMessage()) as $line) {
diag($line);
}
}
}