r2002 + update RackCode parser to match the current language definition
authorDenis Ovsienko <infrastation@yandex.ru>
Wed, 25 Jun 2008 19:12:21 +0000 (19:12 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Wed, 25 Jun 2008 19:12:21 +0000 (19:12 +0000)
inc/code.php
inc/config.php
inc/functions.php

index a4c14dd5736395336c0974356e53e7a58915daa0..124a254300db194f2edcbe5aed26f472065e62a8 100644 (file)
@@ -18,7 +18,7 @@
  */
 
 // Complain about martian char.
-function abortLex1 ($state, $text, $pos)
+function lexError1 ($state, $text, $pos)
 {
        $message = "invalid char with code " . ord (mb_substr ($text, $pos, 1));
        $message .= " at position ${pos} (FSM state is '${state}')";
@@ -26,7 +26,7 @@ function abortLex1 ($state, $text, $pos)
 }
 
 // Complain about martian keyword.
-function abortLex2 ($word)
+function lexError2 ($word)
 {
        return array
        (
@@ -36,7 +36,7 @@ function abortLex2 ($word)
 }
 
 // Complain about wrong FSM state.
-function abortLex3 ($state)
+function lexError3 ($state)
 {
        return array
        (
@@ -45,6 +45,15 @@ function abortLex3 ($state)
        );
 }
 
+function lexError4 ($s)
+{
+       return array
+       (
+               'result' => 'NAK',
+               'load' => "Invalid tag or predicate name '${s}'"
+       );
+}
+
 // Produce a list of lexems from the given text. Possible lexems are:
 function getLexemsFromRackCode ($text)
 {
@@ -81,7 +90,7 @@ function getLexemsFromRackCode ($text)
                                                $newstate = 'reading predicate 1';
                                                break;
                                        default:
-                                               return abortLex1 ($state, $text, $i);
+                                               return lexError1 ($state, $text, $i);
                                }
                                break;
                        case 'reading keyword':
@@ -113,12 +122,12 @@ function getLexemsFromRackCode ($text)
                                                                $ret[] = array ('type' => 'LEX_BOOLCONST', 'load' => $buffer);
                                                                break;
                                                        default:
-                                                               return abortLex2 ($buffer);
+                                                               return lexError2 ($buffer);
                                                }
                                                $newstate = 'ESOTSM';
                                                break;
                                        default:
-                                               return abortLex1 ($state, $text, $i);
+                                               return lexError1 ($state, $text, $i);
                                }
                                break;
                        case 'reading tag 1':
@@ -127,12 +136,12 @@ function getLexemsFromRackCode ($text)
                                        case (preg_match ('/^[ \t\n\r]$/', $char)):
                                                // nom-nom...
                                                break;
-                                       case (mb_ereg ('[[:alpha:]\$]', $char) > 0):
+                                       case (mb_ereg ('[[:alnum:]\$]', $char) > 0):
                                                $buffer = $char;
                                                $newstate = 'reading tag 2';
                                                break;
                                        default:
-                                               return abortLex1 ($state, $text, $i);
+                                               return lexError1 ($state, $text, $i);
                                }
                                break;
                        case 'reading tag 2':
@@ -140,16 +149,16 @@ function getLexemsFromRackCode ($text)
                                {
                                        case ($char == '}'):
                                                $buffer = rtrim ($buffer);
-                                               if (mb_ereg ('[[:alnum:]]', mb_substr ($buffer, -1)) == 0)
-                                                       return abortLex1 ($state, $text, $i);
+                                               if (!validTagName ($buffer, TRUE))
+                                                       return lexError4 ($buffer);
                                                $ret[] = array ('type' => 'LEX_TAG', 'load' => $buffer);
                                                $newstate = 'ESOTSM';
                                                break;
-                                       case (mb_ereg ('[[:alnum:] _-]', $char) > 0):
+                                       case (mb_ereg ('[[:alnum:]\. _-]', $char) > 0):
                                                $buffer .= $char;
                                                break;
                                        default:
-                                               return abortLex1 ($state, $text, $i);
+                                               return lexError1 ($state, $text, $i);
                                }
                                break;
                        case 'reading predicate 1':
@@ -158,12 +167,12 @@ function getLexemsFromRackCode ($text)
                                        case (preg_match ('/^[ \t\n\r]$/', $char)):
                                                // nom-nom...
                                                break;
-                                       case (mb_ereg ('[[:alpha:]]', $char) > 0):
+                                       case (mb_ereg ('[[:alnum:]]', $char) > 0):
                                                $buffer = $char;
                                                $newstate = 'reading predicate 2';
                                                break;
                                        default:
-                                               return abortLex1 ($state, $text, $i);
+                                               return lexError1 ($state, $text, $i);
                                }
                                break;
                        case 'reading predicate 2':
@@ -171,16 +180,16 @@ function getLexemsFromRackCode ($text)
                                {
                                        case ($char == ']'):
                                                $buffer = rtrim ($buffer);
-                                               if (mb_ereg ('[[:alnum:]]', mb_substr ($buffer, -1)) == 0)
-                                                       return abortLex1 ($state, $text, $i);
+                                               if (!validTagName ($buffer))
+                                                       return lexError4 ($buffer);
                                                $ret[] = array ('type' => 'LEX_PREDICATE', 'load' => $buffer);
                                                $newstate = 'ESOTSM';
                                                break;
-                                       case (mb_ereg ('[[:alnum:] _-]', $char) > 0):
+                                       case (mb_ereg ('[[:alnum:]\. _-]', $char) > 0):
                                                $buffer .= $char;
                                                break;
                                        default:
-                                               return abortLex1 ($state, $text, $i);
+                                               return lexError1 ($state, $text, $i);
                                }
                                break;
                        case 'skipping comment':
@@ -198,7 +207,7 @@ function getLexemsFromRackCode ($text)
                $state = $newstate;
        endfor;
        if ($state != 'ESOTSM' and $state != 'skipping comment')
-               return abortLex3 ($state);
+               return lexError3 ($state);
        return array ('result' => 'ACK', 'load' => $ret);
 }
 
index 8b9dc8e11aa7d51ed43d8b44f71f79012c9c3d0d..03ce4e1781e17dbbf88f9bd0f4bc110a5f712670 100644 (file)
@@ -18,6 +18,7 @@ define ('CODE_VERSION', '0.16.0');
 define ('PASSWORD_HASH', 'sha1');
 
 define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _-]?[[:alnum:]])*$');
+define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _-]?[[:alnum:]])*$');
 
 function getConfigVar ($varname = '')
 {
index 7c35dbf7a7d3edb432f089666f45a0054b4e5901..8b2beafee7cf53ed013ba6d5d34d70c75130711e 100644 (file)
@@ -1566,9 +1566,13 @@ function buildRedirectURL_ERR ($text, $p = NULL, $t = NULL)
        return buildWideRedirectURL (array (array ('code' => 'error', 'message' => $text)), $p, $t);
 }
 
-function validTagName ($s)
+function validTagName ($s, $allow_autotag = FALSE)
 {
-       return 1 == mb_ereg (TAGNAME_REGEXP, $s);
+       if (1 == mb_ereg (TAGNAME_REGEXP, $s))
+               return TRUE;
+       if ($allow_autotag and 1 == mb_ereg (AUTOTAGNAME_REGEXP, $s))
+               return TRUE;
+       return FALSE;
 }
 
 ?>