Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between : and :: and ::: in Javascript grammar

In the ECMAScript grammar specification for Javascript, there are blocks defined with a double colon like this:

Literal ::
    NullLiteral
    BooleanLiteral
    NumericLiteral
    StringLiteral
    RegularExpressionLiteral

And blocks defined with a single colon like this:

PrimaryExpression :
    this
    Identifier
    Literal
    ArrayLiteral
    ObjectLiteral
    ( Expression )

And, even blocks with a triple colon:

uriCharacter :::
    uriReserved
    uriUnescaped
    uriEscaped

What is the difference between the single and double and triple colons?

like image 968
jfriend00 Avatar asked Mar 28 '15 01:03

jfriend00


2 Answers

The JSON lexical grammar is used to translate character sequences into tokens and is similar to parts of the ECMAScript lexical grammar. The JSON syntactic grammar describes how sequences of tokens from the JSON lexical grammar can form syntactically correct JSON object descriptions.

Lexical rules ("::") for tokens mean "what the parts of the language look like". It defines rules like "5.5 is a number".

Syntactic rules (":") for expressions mean "how the parts interact with eachother". It defines rules like "5.5 abc doesn't make sense".

The triple-colon (":::") seems to be reserved specifically to define rules for converting strings to numbers. The string " 0x1235 " (with whitespace) is a valid number. The triple-colon rules define that.

The triple-colon (":::") also seems to be used for uri string grammar. Most commonly used like this: "f%20o%20o" decodes to "f o o". These rules define the structure of the "numerical" part of the strings.

like image 68
Joe Frambach Avatar answered Oct 15 '22 05:10

Joe Frambach


See Standard ECMA-262 5.1 Edition / June 2011/

5.1.1 Context-Free Grammars

A context-free grammar consists of a number of productions. Each production has an abstract symbol called a nonterminal as its left-hand side, and a sequence of zero or more nonterminal and terminal symbols as its right-hand side. For each grammar, the terminal symbols are drawn from a specified alphabet.

Starting from a sentence consisting of a single distinguished nonterminal, called the goal symbol, a given context-free grammar specifies a language, namely, the (perhaps infinite) set of possible sequences of terminal symbols that can result from repeatedly replacing any nonterminal in the sequence with a right-hand side of a production for which the nonterminal is the left-hand side.

5.1.6 Grammar Notation

Terminal symbols of the lexical and string grammars, and some of the terminal symbols of the syntactic grammar, are shown in fixed width font, both in the productions of the grammars and throughout this specification whenever the text directly refers to such a terminal symbol. These are to appear in a program exactly as written. All terminal symbol characters specified in this way are to be understood as the appropriate Unicode character from the ASCII range, as opposed to any similar-looking characters from other Unicode ranges.

Nonterminal symbols are shown in italic type. The definition of a nonterminal is introduced by the name of the nonterminal being defined followed by one or more colons. (The number of colons indicates to which grammar the production belongs.) One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. For example, the syntactic definition:

   WhileStatement :
     while(Expression) Statement

states that the nonterminal WhileStatement represents the token while, followed by a left parenthesis token, followed by an Expression, followed by a right parenthesis token, followed by a Statement. The occurrences of Expression and Statement are themselves nonterminals. As another example, the syntactic definition:

   ArgumentList :
     AssignmentExpression
     ArgumentList , AssignmentExpression

:::

Productions of the numeric string grammar are distinguished by having three colons ":::" as punctuation.

::

Productions of the lexical and RegExp grammars are distinguished by having two colons "::" as separating punctuation. The lexical and RegExp grammars share some productions.

:

Productions of the syntactic grammar are distinguished by having just one colon ":" as punctuation.

Note,

5.1.5 The JSON Grammar

Productions of the JSON lexical grammar are distinguished by having two colons "::" as separating punctuation. The JSON lexical grammar uses some productions from the ECMAScript lexical grammar. The JSON syntactic grammar is similar to parts of the ECMAScript syntactic grammar. Productions of the JSON syntactic grammar are distinguished by using one colon ":" as separating punctuation.

like image 1
guest271314 Avatar answered Oct 15 '22 03:10

guest271314