「javascript」spreadSheet4 EBNFの「,」

やっとマクロ構文を組み込み始めたが・・・

デリミタではまった。

(*$ imports: expression="EXPR" $*)
(*$ SEQUENCE SEPARATOR IS REQUIRED $*)
(* マクロ *)
macro               = { ( class declaration | function declaration | variable declaration ) }, /\s*/ ;
class declaration   = "class", class name, "{", { member declaration | method declaration } "}" ;
class name          = identifier ;
comment             = "/*", /.*/, "*/" | "//", /.*/, "\n" ;
member declaration  = [ "const" | "static" ], member name, [ "=", expression], { ",", member name, [ "=", expression] } ";" ;
member name         = identifier ;
method declaration  = [ ? ebnf reference jdoc.document ? ], [ method name ], parameter list, procedure list ;
method name         = identifier ;
function declaration= [ ? ebnf reference jdoc.document ? ], [ function name ], parameter list, procedure list ;
function name       = identifier ;
parameter list      = "(", [ parameter name, [ "=", expression ], { ",", parameter name, [ "=", expression ] } ], ")" ;
parameter name      = identifier ;
procedure list      = "{", procedure, "}";
procedure           =    {
                         (
                         | variable declaration 
                         | assignment expression
                         | branch procedure
                         | iterative procedure
                         | exception procedure
                         )
                      } ;
variable declaration= [ "const" | "let" | "static" ], variable name, [ "=", expression], { ",", variable name, [ "=", expression] } ";" ;
variable name       = identifier ;
assignment expression= variable name, [ ".", member name ] "=" expression;
branch procedure    = if procedure | switch procedure ;
if procedure        = "if", "(", expression, ")", procedure list, { "else", if procedure } ;
switch procedure    = "switch", "{", { case procedure } "}" ;
case procedure      = ( "case", expression | "default" ), ":", { procedure }, [ "break" ] ;
iterative procedure = for procedure | while procedure | do while procedure ;
for procedure       = "for", "(", assignment expression, ",",  expression  ",",  assignment expression ")" ;
while procedure     = "while", "(", expression ")", procedure list ;
do while procedure  = "do", "(", expression ")", procedure list, "while", "(", expression ")" ;
exception procedure = "try", procedure list, "catch", "(", parameter name ")", procedure list, [ "finally", procedure list ]
                    |  "throw", expression ;
identifier          = { comment }, /[A-Za-z][A-Z_a-z]/, { comment } ;

どこが間違っているのかな?

(*$ import expr from "EXPR" $*)
(*$ SEQUENCE SEPARATOR IS REQUIRED $*)
(* マクロ *)
macro               = { class declaration | function declaration | variable declaration }, /\s*/ ;
class declaration   = "class", class name, "{", member declaration, { method declaration }, "}" ;
class name          = identifier ;
comment             = "/*", /.*/, "*/" | "//", /.*/, "\n" ;
member declaration  = [ "const" | "static" ], member name, [ "=", expression], { ",", member name, [ "=", expression] }, ";" ;
member name         = identifier ;
method declaration  = [ ? ebnf reference jdoc.document ? ], [ method name ], parameter list, procedure list ;
method name         = identifier ;
function declaration= [ ? ebnf reference jdoc.document ? ], [ function name ], parameter list, procedure list ;
function name       = identifier ;
parameter list      = "(", [ parameter name, [ "=", expression ], { ",", parameter name, [ "=", expression ] } ], ")" ;
parameter name      = identifier ;
procedure list      = "{", procedure, "}";
procedure           =    { variable declaration 
                         | assignment expression
                         | branch procedure
                         | iterative procedure
                         | exception procedure
                         } ;
variable declaration= [ "const" | "let" | "static" ], variable name, [ "=", expression], { ",", variable name, [ "=", expression] }, ";" ;
variable name       = identifier ;
assignment expression= variable name, [ ".", member name ],"=", expression;
branch procedure    = if procedure | switch procedure ;
if procedure        = "if", "(", expression, ")", procedure list, { "else", if procedure } ;
switch procedure    = "switch", "{", { case procedure }, "}" ;
case procedure      = ( "case", expression | "default" ), ":", { procedure }, [ "break" ] ;
iterative procedure = for procedure | while procedure | do while procedure ;
for procedure       = "for", "(", assignment expression, ",", expression, ",", assignment expression, ")" ;
while procedure     = "while", "(", expression, ")", procedure list ;
do while procedure  = "do", "(", expression, ")", procedure list, "while", "(", expression, ")" ;
expression          = expr ;
exception procedure = "try", procedure list, "catch", "(", parameter name, ")", procedure list, [ "finally", procedure list ]
                    |  "throw", expression ;
identifier          = { comment }, /[A-Za-z][A-Z_a-z]/, { comment } ;

違いが判るだろうか?

最初は [ … […] … ] なんて囲み文字のネストは考えて無かったw!とか慌てたけど、そんなことを気にする様な気難しい文法解析方法は今回実装していない。

正解は・・・

  • importが旧仕様
    • ☓ imports: expression=“EXPR”
    • 〇 import expr from “EXPR”
  • sequenceのデリミタ(カンマ)がところどころ抜けている
    • 「}」の後
      • { … } , “;”
        • 「}」の後にトークンがあるなら、カンマで区切る必要がある
    • 「”}”」や「”)”」の前
      • { … } の } の前にカンマは不要、 ( … ) なら ) の前にカンマは不要
      • “{” … “}” の “}” の前にカンマは必須、 “(” … “)” なら “)” の前にカンマは必須

いづれも独自の仕様と云えば独自なのかもしれないけどね。

ps.import の処理が間違っていたので修正。EbnfParserクラスのparseメソッドを実行すると、export、importの情報が履歴の様に残っていたので、parseメソッド内でクリアするように変えるのでシャローコピーを各パーサに引き渡す様にした。

ps.2024/4/25

マクロファイルのアップロード、パーサ、indexedDB処理までできた。後はパーサのMap処理と実行部。どうすればいいのかは・・・やってみないと判らない。(笑




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA