Vous êtes sur la page 1sur 6

NewtonScript Syntax Definition Version 1.

0
Copyright 1994 Apple Computer, Inc. All Rights Reserved.

Introduction
The definitions in this document are in a form of extended BNF defined as follows: nonterminal terminal, possibly 'quoted' for ambiguous characters [ optional ] { choose | one | of | these } [ zero or more ]* [ one or more ]+ <descriptions of characters> The grammar is divided into two parts. In the phrasal grammar, whitespace is insignificant. Space, tab, return, and linefeed characters are considered whitespace. Comments are effectively considered whitespace. Comments consist of the characters between /* and */ (not nested), and between // and a return or linefeed character. In the lexical grammar, the nonterminals are characters rather than tokenswhitespace is significant. Because almost every construct of the language is an expression, many productions ending in expression are ambiguous; the ambiguity is resolved in favor of extending the expression as long as possible. For example, while true do 2+2 is parsed as while true do (2+2) rather than (while true do 2)+2. The specific productions affected by this rule are function-constructor, assignment, iteration, if-expression, breakexpression, try-expression, initialization-clause, return-expression , and global-function-decl .

Phrasal Grammar
input [ constituent [ ; constituent ]* [ ; ] ] constituent { expression | global-declaration } expression { simple-expression | compound-expression | literal | constructor | lvalue | assignment | existsexpression | function-call | message-send | if-expression | iteration | break-expression | tryexpression | local-declaration | constant-declaration | return-expression } simple-expression { expression binary-operator expression | unary-operator expression | ( expression ) | self } binary-operator { arithmetic-operator | relational-operator | boolean-operator | string-operator } arithmetic-operator { + | - | * | / | div | mod | << | >> } relational-operator { = | <> | < | > | <= | >= }

NewtonScript Syntax Definition Version 1.0

boolean-operator { and | or } string-operator { & | && } unary-operator { - | not } compound-expression begin expression-sequence end expression-sequence [ expression [ ; expression ]* [ ; ] ] literal { simple-literal | ' object } simple-literal { string | integer | real | character | true | nil } object { simple-literal | path-expression | array | frame } path-expression symbol [ . symbol ]+ NOTE: Each dot in ' symbol . symbol ... is ambiguous: it could be a continuation of the path expression, or a slot accessor. NewtonScript uses the first interpretation: 'x.y.z is one long path expression, not ('x).y.z. array '[' [ symbol : ] [ object [ , object ]* [ , ] ] ']' frame '{' [ frame-slot [ , frame-slot ]* [ , ] ] '}' frame-slot symbol : object constructor { array-constructor | frame-constructor | function-constructor } array-constructor '[' [ symbol : ] [ expression [ , expression ]* [ , ] ] ']' NOTE: '[' symbol : symbol ( is ambiguous: the first symbol could be a class for the array, or a variable to be used as the receiver for a message send. NewtonScript uses the first interpretation. frame-constructor '{' [ frame-constructor-slot [ , frame-constructor-slot ]* [ , ] ] '}' frame-constructor-slot symbol : expression function-constructor func ( [ formal-argument-list ] ) expression

NewtonScript Syntax Definition Version 1.0

formal-argument-list symbol [ , symbol ]* lvalue { symbol | frame-accessor | array-accessor } frame-accessor expression . { symbol | ( expression ) } array-accessor expression '[' expression ']' assignment lvalue := expression exists-expression { symbol | frame-accessor | [ expression ] : symbol } exists function-call { symbol ( [ actual-argument-list ] ) | call expression with ( [ actual-argument-list ] ) } actual-argument-list expression [ , expression ]* message-send [ { expression | inherited } ] { : | :? } symbol ( [ actual-argument-list ] ) if-expression if expression then expression [ ; ] [ else expression ] NOTE: An else clause is associated with the most recent unmatched then clause. iteration { infinite-loop | for-loop | foreach-loop | while-loop | repeat-loop } infinite-loop loop expression for-loop for symbol := expression to expression [ by expression ] do expression foreach-loop foreach symbol [ , symbol ] [ deeply ] in expression { do | collect } expression while-loop while expression do expression repeat-loop repeat expression-sequence until expression break-expression break [ expression ] try-expression try expression-sequence [ onexception symbol do expression [ ; ] ]+

NewtonScript Syntax Definition Version 1.0

local-declaration local initalization-clause [ , initalization-clause ]* initialization-clause symbol [ := expression ] constant-declaration constant constant-init-clause [ , constant-init-clause ]* constant-init-clause symbol := expression return-expression return [ expression ] global-declaration { global initialization-clause | global-function-decl } global-function-decl { global | func } symbol ( [ formal-argument-list ] ) expression

Lexical grammar
string " character-sequence " character-sequence [ { string-character | escape-sequence } ]* [ truncated-escape ] string-character <tab or any ASCII character with code 32127 except '"' or '\'> escape-sequence { \ { " | \ | n | t } | \ u [ hex-digit hex-digit hex-digit hex-digit ]* \ u } truncated-escape \ u [ hex-digit hex-digit hex-digit hex-digit ]* symbol { { alpha | _ } [ { alpha | digit | _ } ]* | '|' [ { symbol-character | \ { '|' | \ } ]* '|' } NOTE: Reserved words are excluded from the nonterminal symbol. symbol-character <any ASCII character with code 32127 except '|' or '\'> integer [ - ] { [ digit ]+ | 0x [ hex-digit ]+ } real [ - ] [ digit ]+ . [ digit ]* [ { e | E } [ - ] [ digit ]+ ] character $ { non-escape-character | \ { \ | n | t | hex-digit hex-digit | u hex-digit hex-digit hex-digit hex-digit } }

NewtonScript Syntax Definition Version 1.0

non-escape-character <any ASCII character with code 32127 except '\'> alpha <AZ and az> digit {0|1|2|3|4|5|6|7|8|9} hex-digit { digit | a | b | c | d | e | f | A | B | C | D | E | F }

Operator precedence
The following table shows the operators in order of precedence from highest to lowest. Operators on the same line have equal precedence. All operators associate left-to-right, except exists, which does not associate, and :=, which is right-to-left. . : :? [] - (unary) << >> * / + & && exists < <= not and or :=

div

mod

>

>=

<>

Real life
There are differences between this language definition and the language accepted by the compiler in Newton Toolkit 1.0 for Macintosh. These differences are considered either bugs or extensions, as follows: Extensions The compiler accepts the 8-bit extended Macintosh character set in strings, and translates characters with codes >= 128 into the equivalent Unicode characters. Such characters may have different translations, or may not be accepted at all, by other NewtonScript implementations. The compiler accepts return characters in strings. This is arguably a bug, since it makes it hard to figure out that you've accidentally omitted the final quote of a string; however, in honor of existing code, we call it an extension. The compiler accepts the syntax "# [ hex-digit ]+" for translating internal reference numbers into object references. This is an extension for debugging purposes that is inherently unsafe, and not part of the formal NewtonScript definition. The compiler does not allow the use of global variable declarations; they are anti-social when used in the Newton environment. Bugs

NewtonScript Syntax Definition Version 1.0

The compiler allows the use of \u escape sequences in symbols when surrounded by vertical bars (|). This is in poor taste and can result in indeterminate behavior. The compiler accepts any character after a backslash (\) in a string, symbol, or character literal. This behavior should not be relied on, in case future special escape characters are introduced.

NewtonScript Syntax Definition Version 1.0

Vous aimerez peut-être aussi