Dylan Design Notes

#22: BNF for Infix Dylan	(Change)

Version 1, January 1994
Copyright (c) 1993-1994, Apple Computer

This document presents a preliminary specification of the lexical and 
syntactic aspects of  Dylan.

-------------------------------------------------------------------

Lexical notes

In the lexical grammar, the various elements that come together on the 
right-hand sides of rules must not be separated by white-space, because 
the end result will be a single token. This is in contrast to the phrase 
grammar, where each element is already a complete token or a series of 
complete tokens.

Arbitrary white-space is permitted between tokens, but it is required 
only as necessary to separate tokens that might otherwise blend together.

Case is not significant except within character and string literals. The 
grammars do not reflect this, using one case or the other, but it is 
still true.

Bracket-style comments may be nested.

Note: In this grammar, the suffix OPT means that the element is optional.


LEXICAL GRAMMAR
---------------

GENERAL

token:
	symbol
	keyword
	literal
	binary-operator
	punctuation
symbol:
	any symbol that is not also a reserved-word
	`  symbol  `
	`  operator  `
keyword:
	symbol  :
literal:
	number
	character-literal
	string-literal
	#`  symbol  `
	#`  operator  `
binary-operator:
	any operator that is not also punctuation
punctuation:
	one of	( ) , . ; [ ] { } :: - ~ = == => #( #[
	#t
	#f
	#next
	#rest
	#key
comment:
	#   ...the rest of the line
	##  ...the rest of the line
	#{  ...everything even across lines... #}


OPERATORS

initial-character:
	one of	! & * + / < = > ? | ^ $ % @ _
noninitial-character:
	one of	- ~
operator-character:
	initial-character
	noninitial-character
operator-name:
	initial-character
	operator-name  operator-character
operator:
	operator-name
	one of	- ~ :=

SYMBOLS

alphanumeric-character:
	one of	a b c d e f g h i j k l m n o p q r s t u v w x y z
		0 1 2 3 4 5 6 7 8 9
symbol-name:
	alphanumeric-character
	symbol-name  alphanumeric-character
	symbol-name  operator-character
	operator-name  symbol-name
symbol:
	any symbol-name that is not also a number
reserved-word:
	one of above, begin, below, block, by, case, class, cleanup,
	constant, define, else, end, exception, finally, for, from,
	generic, handler, if, in, instance, let, local, method,
	otherwise, select, slot, subclass, then, to, unless, until,
	variable, virtual, while

NUMBERS

number:
	integer
	ratio
	floating-point
integer:
	binary-integer
	octal-integer
	signOPT  decimal-integer
	hex-integer
binary-integer:
	#b  binary-digit
	binary-integer  binary-digit
octal-integer:
	#o  octal-digit
	octal-integer  octal-digit
decimal-integer:
	decimal-digit
	decimal-integer  decimal-digit
hex-integer:
	#x  hex-digit
	hex-integer  hex-digit
binary-digit:
	one of	0 1
octal-digit:
	one of	0 1 2 3 4 5 6 7
decimal-digit:
	one of	0 1 2 3 4 5 6 7 8 9
hex-digit:
	one of	0 1 2 3 4 5 6 7 8 9 A B C D E F
ratio:
	signOPT  decimal-integer  /  decimal-integer
floating-point:
	signOPT  decimal-integerOPT  .  decimal-integer  exponentOPT
	signOPT  decimal-integer  .  decimal-integerOPT  exponentOPT
	signOPT  decimal-integer  exponent
exponent:
	E  signOPT  decimal-integer
sign:
	one of	+ -

CHARACTER AND STRING LITERALS

character-literal:
	'  normal-character  '
	'\  special-character  '
normal-character:
	any printing character (including space) except for  '  or  \
special-character:
	one of	' \ b f n r t
string-literal:
	"  more-string
more-string:
	"
	normal-string-character  more-string
	\ special-string-character  more-string
normal-string-character:
	any printing character (including space) except for  "  or  \
special-string-character:
	one of	" \ b f n r t

PHRASE GRAMMAR
--------------

PROGRAM STRUCTURE

dylan-program:
	body
body:
	constituents  ;OPT
constituents:
	constituent
	constituents  ;  constituent
constituent:
	defining-form
	local-declaration
	expression
defining-form:
	define  class  class-definition
	define  constant  bindings
	define  generic  generic-function-definition
	define  method  named-method
	define  variable  bindings
local-declaration:
	let  bindings
	let  handler  symbol  =  expression
	let  handler  (  expression  property-listOPT  )  =  expression
	local  local-methods 
bindings:
	variables  =  expression
	(  variables  )  =  expression
variables:
	#rest  symbol
	variable
	variable  ,  variables
variable:
	symbol
	symbol  ::  operand
local-methods:
	local-method
	local-methods  ,  local-method
local-method:
	methodOPT  named-method

EXPRESSIONS

expression:
	keyword
	operand
	operand  binop-series
binop-series:
	binop  operand
	binop-series  binop  operand
binop:
	binary-operator
	-
	=
	==
operand:
	-  operand
	~  operand
	leaf
leaf:
	constant
	symbol
	leaf  [  argumentsOPT  ]
	leaf  (  argumentsOPT  )
	anonymous-method
	leaf  .  symbol
	(  expression  )
	statement
arguments:
	argument
	arguments  ,  argument
argument:
	keywordOPT  expression
property-list:
	,  keyword  expression
	property-list  ,  keyword  expression
return-type-list:
	#rest  expression
	expression  ,  expression
	expression  ,  return-type-list

CONSTANTS

constant:
	literal
	#t
	#f
	#(  dotted-list  )
	#(  bare-literalsOPT  )
	#[  bare-literalsOPT  ]
dotted-list:
	bare-literals  .  bare-literal
bare-literals:
	bare-literal
	bare-literals  ,  bare-literal
bare-literal:
	(  dotted-list  )
	(  bare-literalsOPT  )
	[  bare-literalsOPT  ]
	constant
	symbol
	keyword

STATEMENTS

statement:
	begin  bodyOPT  end
	block  (  symbolOPT  )  body  block-epilogOPT  end  blockOPT
	case  condition-body  end  caseOPT
	if  (  expression  )  body  else-partOPT  end  ifOPT
	for  (  for-header  )  bodyOPT  final-partOPT  end  forOPT
	select  (  expression  by-partOPT  )  condition-body  end  selectOPT
	unless  (  expression  )  body  end  unlessOPT
	until  (  expression  )  bodyOPT  end  untilOPT
	while  (  expression  )  bodyOPT  end  whileOPT
block-epilog:
	exception-clausesOPT  cleanup-part  exception-clausesOPT
	exception-clauses
condition-body:
	complete-condition-clauses
for-header:
	until  expression
	while  expression
	for-clause
	for-clause  ,  for-header

STATEMENT CLAUSES

exception-clauses:
	exception-clause
	exception-clauses  exception-clause
exception-clause:
	exception  symbol  body
	exception  (  expression  property-listOPT  )  body
	exception  (  symbol  ::  expression  property-listOPT  )  body
condition-clauses:
	complete-condition-clauses
	incomplete-condition-clauses
complete-condition-clauses:
	otherwise  =>OPT  body
	condition-clause  ;OPT
	condition-clause  ;  condition-clauses
incomplete-condition-clauses:
	constituent  ;OPT
	constituent  ;  condition-clauses
condition-clause:
	expression  =>  constituent
	expression  ,  condition-clause
for-clause:
	variable  =  expression  then  expression
	variable  in  expression
	variable  from  expression  to-partOPT  by-partOPT
by-part:
	by  expression
cleanup-part:
	cleanup  body
else-part:
	else  body
final-part:
	finally  body
to-part:
	to  expression
	above  expression
	below  expression

CLASSES

class-definition:
	symbol  (  superclasses  )  slot-specsOPT  end  classOPT  symbolOPT
superclasses:
	expression
	superclasses  ,  expression
slot-specs:
	slot-spec  ;OPT
	slot-spec  ;  slot-specs
slot-spec:
	allocationOPT  slot  symbolOPT  slot-typeOPT  property-listOPT
allocation:
	instance
	class
	subclass
	constant
	virtual
slot-type:
	::  expression

GENERIC FUNCTIONS

generic-function-definition:
	symbol  (  gf-parametersOPT  )  gf-suffix
gf-suffix:
	property-listOPT
	::  expression  property-listOPT
	::  (  return-type-list  )  property-listOPT
	::  return-type-list
gf-parameters:
	gf-positional-parameters
	gf-positional-parameters  ,  gf-rest-parameters
	gf-rest-parameters
gf-positional-parameters:
	symbol
	gf-positional-parameters  ,  symbol
gf-rest-parameters:
	rest-parameter
	rest-parameter  ,  #key  gf-keyword-parametersOPT
	#key  gf-keyword-parametersOPT
gf-keyword-parameters:
	keyword
	gf-keyword-parameters  ,  keyword

METHODS

anonymous-method:
	method  method-description  end  methodOPT
named-method:
	symbol  method-description  end  methodOPT  symbolOPT
method-description:
	(  parametersOPT  )  return-type  ;  body
	(  parametersOPT  )  return-type  ;OPT
	(  parametersOPT  )  bodyOPT
return-type:
	::  expression
	::  return-type-list
	::  (  return-type-list  )
parameters:
	positional-parameters
	positional-parameters  ,  next-parameters
	next-parameters
positional-parameters:
	positional-parameter
	positional-parameters  ,  positional-parameter
positional-parameter:
	symbol
	symbol  ::  expression
	symbol  ==  expression
next-parameters:
	next-parameter
	next-parameter  ,  rest-parameters
	rest-parameters
next-parameter:
	#next  symbol
rest-parameters:
	rest-parameter
	rest-parameter  ,  #key  keyword-parametersOPT
	#key  keyword-parametersOPT
rest-parameter:
	#rest  symbol
keyword-parameters:
	keyword-parameter
	keyword-parameters  ,  keyword-parameter
keyword-parameter:
	keyword  symbol
	keyword  symbol  (  expression  )
