From cb772298180a79742ea57c7309210eb35cc6cff6 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 8 Oct 2020 14:51:39 -0400 Subject: Add grammar for namespace blocks with export and import statements --- docs/spec/grammar.html | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'docs/spec') diff --git a/docs/spec/grammar.html b/docs/spec/grammar.html index b5443ce7..8eabe899 100644 --- a/docs/spec/grammar.html +++ b/docs/spec/grammar.html @@ -7,9 +7,9 @@

Specification: BQN grammar

BQN's grammar is given below. Terms are defined in a BNF variant. However, handling special names properly is possible but difficult in BNF, so they are explained in text along with the braced block grammar.

The symbols s, F, _m, and _c_ are identifier tokens with subject, function, 1-modifier, and 2-modifier classes respectively. Similarly, sl, Fl, _ml, and _cl_ refer to literals and primitives of those classes. While names in the BNF here follow the identifier naming scheme, this is informative only: syntactic classes are no longer used after parsing and cannot be inspected in a running program.

-

A program is a list of statements. Almost all statements are expressions. Only valueless results stemming from ยท, or ๐•จ in a monadic brace function, can be used as statements but not expressions.

-
PROGRAM  = โ‹„? ( ( STMT โ‹„ )* STMT โ‹„? )?
-STMT     = EXPR | nothing
+

A program is a list of statements. Almost all statements are expressions. Valueless results stemming from ยท, or ๐•จ in a monadic brace function, can be used as statements but not expressions. "Namespace statements", which import multiple values from a namespace block (immediate block containing โ‡), also cannot be expressions. An extension to BQN to allow first-class namespaces would extend ordinary expressions so that NS_STMT would no longer be needed, as it would be a subset of EXPR.

+
PROGRAM  = โ‹„? ( ( STMT | EXPORT โ‹„ )* STMT โ‹„? )?
+STMT     = EXPR | nothing | NS_STMT
 โ‹„        = ( "โ‹„" | "," | \n )+
 EXPR     = subExpr | FuncExpr | _m1Expr | _m2Expr_
 
@@ -22,8 +22,8 @@ list = "โŸจ" โ‹„? ( ( EXPR โ‹„ )* EXPR โ‹„? )? "โŸฉ" subject = atom | ANY ( "โ€ฟ" ANY )+
-

Starting at the highest-order objects, modifiers have fairly simple syntax. In most cases the syntax for โ† and โ†ฉ is the same, but only โ†ฉ can be used for modified assignment.

-
ASGN     = "โ†" | "โ†ฉ"
+

Starting at the highest-order objects, modifiers have fairly simple syntax. In most cases the syntax for โ† and โ†ฉ is the same, but only โ†ฉ can be used for modified assignment. The export arrow โ‡ can only be used in namespace blocks brNS, and the top-level PROGRAM. There it can be used in the same ways as โ†, but it can also be used in a brNS header, or with no expression on the right in an EXPORT statement.

+
ASGN     = "โ†" | "โ‡" | "โ†ฉ"
 _m2Expr_ = _mod2_
          | _c_ ASGN _m2Expr_
 _m1Expr  = _mod1
@@ -50,13 +50,13 @@
          | ( subject | nothing )? Derv arg
 nothing  = "ยท"
          | ( subject | nothing )? Derv nothing
-LHS_ANY  = lhsSub | F | _m | _c_
+LHS_NAME = s | F | _m | _c_
+LHS_ANY  = LHS_NAME
+         | "โŸจ" โ‹„? ( ( LHS_ELT โ‹„ )* LHS_ELT โ‹„? )? "โŸฉ"
 LHS_ATOM = LHS_ANY | "(" lhsStr ")"
 LHS_ELT  = LHS_ANY | lhsStr
-lhsSub   = s
-         | "โŸจ" โ‹„? ( ( LHS_ELT โ‹„ )* LHS_ELT โ‹„? )? "โŸฉ"
 lhsStr   = LHS_ATOM ( "โ€ฟ" LHS_ATOM )+
-lhs      = lhsSub | lhsStr
+lhs      = s | lhsSub | lhsStr
 subExpr  = arg
          | lhs ASGN subExpr
          | lhs Derv "โ†ฉ" subExpr       # Modified assignment
@@ -87,7 +87,16 @@
 _brMod1  = "{" ( _mCase  ";" )* ( _mCase  | _mMain ( ";" _mMain )? ) "}"
 _brMod2_ = "{" ( _cCase_ ";" )* ( _cCase_ | _cMan_ ( ";" _cMan_ )? ) "}"
 
-

Two additional rules apply to blocks, based on the special name associations in the table below. First, each block allows the special names in its column to be used as the given token types within BODY terms (not headers). Except for the spaces labelled "None", each column is cumulative and a given entry also includes all the entries above it. Second, for BrFunc, _brMod1, and _brMod2_ terms, if no header is given, then at least one BODY term in it must contain one of the names on, and not above, the corresponding row. Otherwise the syntax would be ambiguous, since for example a simple "{" BODY "}" sequence could have any type.

+

A namespace block is very similar in grammar to an ordinary immediate block, but allows export declarations with โ‡, either in place of the ordinary definition โ† or in the special EXPORT statement. The arrow โ‡ can also be placed in the header to mark a namespace block.

+
NS_STMT  = nsLHS ASGN brNS
+NS_VAR   = LHS_NAME ( ":" lhs )?
+nsLHS    = LHS_NAME ( "โ€ฟ" LHS_NAME )+
+         | "โŸจ" โ‹„? ( ( NS_VAR โ‹„ )* NS_VAR โ‹„? )? "โŸฉ"
+EXPORT   = ( LHS_NAME | lhsSub | lhsStr ) "โ‡"
+NS_BODY  = โ‹„? ( ( STMT | EXPORT ) โ‹„ )* EXPR โ‹„?
+brNS     = "{" ( โ‹„? "โ‡"? s ":" )? NS_BODY "}"
+
+

Two additional rules apply to blocks, based on the special name associations in the table below. First, each block allows the special names in its column to be used as the given token types within BODY terms (not headers). Except for the spaces labelled "None", each column is cumulative and a given entry also includes all the entries above it up to the next "None". Second, for BrFunc, _brMod1, _brMod2_, and brNS terms, if no header is given (or, for brNS, if the header does not contain "โ‡"), then at least one BODY term in it must contain one of the tokens on, and not above, the corresponding row. Otherwise the syntax would be ambiguous, since for example a simple "{" BODY "}" sequence could have any type.

@@ -101,12 +110,20 @@ - + + + + + + + + + + - @@ -114,7 +131,7 @@ - + -- cgit v1.2.3
brSub, PROGRAMbrNS, PROGRAMNoneNoneNoneNoneโ‡
brSubNone None None None None
BrFunc๐•Ž๐•๐•Š ";";
_brMod1