diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-10-08 14:51:39 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-10-08 14:51:39 -0400 |
| commit | cb772298180a79742ea57c7309210eb35cc6cff6 (patch) | |
| tree | 18987177a796659e40cabe52e613f84911b47f60 /spec | |
| parent | 4415f68bf3226463f903884ca768aac9ef52a802 (diff) | |
Add grammar for namespace blocks with export and import statements
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/grammar.md | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/spec/grammar.md b/spec/grammar.md index 25d48295..5829e75a 100644 --- a/spec/grammar.md +++ b/spec/grammar.md @@ -6,10 +6,10 @@ BQN's grammar is given below. Terms are defined in a [BNF](https://en.wikipedia. 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. +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 โ )* STMT โ? )? - STMT = EXPR | nothing + PROGRAM = โ? ( ( STMT | EXPORT โ )* STMT โ? )? + STMT = EXPR | nothing | NS_STMT โ = ( "โ" | "," | \n )+ EXPR = subExpr | FuncExpr | _m1Expr | _m2Expr_ @@ -23,9 +23,9 @@ Here we define the "atomic" forms of functions and modifiers, which are either s 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. +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 = "โ" | "โฉ" + ASGN = "โ" | "โ" | "โฉ" _m2Expr_ = _mod2_ | _c_ ASGN _m2Expr_ _m1Expr = _mod1 @@ -54,13 +54,13 @@ Subject expressions are complicated by the possibility of list assignment. We al | ( 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 @@ -93,12 +93,23 @@ A braced block contains bodies, which are lists of statements, separated by semi _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. | Term | `s` | `F` | `_m` | `_c_` | other |--------------------|--------|--------|---------|----------|------- -| `brSub`, `PROGRAM` | None | None | None | None | -| `BrFunc` | `๐จ๐ฉ๐ค` | `๐๐๐` | | | `";"` +| `brNS`, `PROGRAM` | None | None | None | None | `โ` +| `brSub` | None | None | None | None | None +| `BrFunc` | `๐จ๐ฉ๐ค` | `๐๐๐` | | | `;` | `_brMod1` | `๐๐ฃ` | `๐ฝ` | `_๐ฃ` | | | `_brMod2_` | `๐` | `๐พ` | None | `_๐ฃ_` | |
