Some extra stuff. More pragmas, and better EOF message

master
Richard van Velzen 2011-12-29 17:44:44 +01:00
parent cd0fcae035
commit eddf5d0259
5 changed files with 33 additions and 8 deletions

View File

@ -7,8 +7,11 @@ to grammar
: {$$=array();}
toklist sym : $$[] = $2;
toklist lit : $$[] = $2;
toklist num : $$[] = $2;
toklist str : $$[] = substr($2, 1, -1);
to toklist
sym '=' rhs : $$ = new lime_rewrite($1); $$->add_rhs($3);
sym ':' rhs : $$ = new lime_rewrite($1); $$->add_rhs($3);
rewrite '|' rhs : $$->add_rhs($3);
to rewrite
list : $$ = new lime_rhs($1, '');

View File

@ -355,9 +355,10 @@ class state {
}
class sym {
public function __construct($name, $id) {
public function __construct($name, $id, $desc = null) {
$this->name = $name;
$this->id = $id;
$this->desc = $desc;
$this->term = true; // Until proven otherwise.
$this->rule = array();
$this->config = array();
@ -374,6 +375,10 @@ class sym {
return $out;
}
public function descr() {
return $this->descr ? $this->descr : $this->name;
}
}
class rule {
@ -689,9 +694,9 @@ class lime {
$sym->rule[] = $r;
}
function sym($str) {
function sym($str, $description = null) {
if (!isset($this->sym[$str])) {
$this->sym[$str] = new sym($str, count($this->sym));
$this->sym[$str] = new sym($str, count($this->sym), $description);
}
return $this->sym[$str];
@ -999,6 +1004,17 @@ class lime {
case 'class':
$this->parser_class = $args[0];
break;
case 'expect':
if (!ctype_digit($args[0])) {
emit(sprintf('Bad expect pragma: %s', $args[0]));
exit(1);
}
$this->expect_conflicts = $args[0];
break;
case 'token':
$this->sym($args[0], @$args[1]);
break;
default:
emit(sprintf('Bad Parser Pragma: (%s)', $type));
exit(1);
@ -1233,7 +1249,7 @@ function lime_bootstrap() {
bug_unless(is_readable($bootstrap));
foreach(file($bootstrap) as $l) {
$a = explode(':', $l, 2);
$a = preg_split('~(?<=\s|^):(?=\s|$)~', $l, 2);
if (count($a) == 2) {
list($pattern, $code) = $a;

Binary file not shown.

View File

@ -34,7 +34,7 @@ ALPHA [a-zA-Z]
DIGIT [0-9]
ALNUM {ALPHA}|{DIGIT}
WORD {ALNUM}|_
STOP "."
STOP "."|";"
SYM {ALPHA}{WORD}*'*
LIT '.'
@ -87,6 +87,10 @@ BLOCKCMT "/*"({CC}|{CX})*{CT}
[[:space:]] {}
{SYM} tok("sym");
{LIT} tok("lit");
{DIGIT} tok("num");
\"{DCHAR}*\" tok("str");
'{SCHAR}*' tok("str");
. lit();
}

View File

@ -41,8 +41,8 @@ class parse_unexpected_token extends parse_error {
}
class parse_premature_eof extends parse_error {
public function __construct() {
parent::__construct('Premature EOF');
public function __construct(array $expect) {
parent::__construct('Premature EOF, expected {' . implode(', ', $expect) . '}');
}
}
@ -221,6 +221,8 @@ class parse_engine {
private function premature_eof() {
$seen = array();
$expect = $this->get_steps();
while ($this->enter_error_tolerant_state()) {
if (isset($seen[$this->state()])) {
// This means that it's pointless to try here.
@ -244,7 +246,7 @@ class parse_engine {
}
}
throw new parse_premature_eof();
throw new parse_premature_eof($expect);
}
private function current_row() {