5 %%% (c) 2015 Straylight/Edgeware
8 %%%----- Licensing notice ---------------------------------------------------
10 %%% This file is part of the Sensble Object Design, an object system for C.
12 %%% SOD is free software; you can redistribute it and/or modify
13 %%% it under the terms of the GNU General Public License as published by
14 %%% the Free Software Foundation; either version 2 of the License, or
15 %%% (at your option) any later version.
17 %%% SOD is distributed in the hope that it will be useful,
18 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 %%% GNU General Public License for more details.
22 %%% You should have received a copy of the GNU General Public License
23 %%% along with SOD; if not, write to the Free Software Foundation,
24 %%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 \chapter{Module syntax} \label{ch:syntax}
28 %%%--------------------------------------------------------------------------
30 Fortunately, Sod is syntactically quite simple. I've used a little slightly
31 unusual notation in order to make the presentation easier to read. For any
34 \item $\epsilon$ denotes the empty nonterminal:
38 \item @[$x$@] means an optional $x$:
40 \syntax{@[$x$@] ::= $\epsilon$ @! $x$}
42 \item $x^*$ means a sequence of zero or more $x$s:
44 \syntax{$x^*$ ::= $\epsilon$ @! $x^*$ $x$}
46 \item $x^+$ means a sequence of one or more $x$s:
48 \syntax{$x^+$ ::= $x$ $x^*$}
50 \item $x$@<-list> means a sequence of one or more $x$s separated
53 \syntax{$x$<-list> ::= $x$ @! $x$<-list> "," $x$}
57 \subsection{Lexical syntax}
58 \label{sec:syntax.lex}
60 Whitespace and comments are discarded. The remaining characters are
61 collected into tokens according to the following syntax.
64 <token> ::= <identifier>
67 \alt <integer-literal>
71 This syntax is slightly ambiguous, and is disambiguated by the \emph{maximal
72 munch} rule: at each stage we take the longest sequence of characters which
75 \subsubsection{Identifiers} \label{sec:syntax.lex.id}
78 <identifier> ::= <id-start-char> @<id-body-char>^*
80 <id-start-char> ::= <alpha-char> | "_"
82 <id-body-char> ::= <id-start-char> @! <digit-char>
84 <alpha-char> ::= "A" | "B" | \dots\ | "Z"
85 \alt "a" | "b" | \dots\ | "z"
86 \alt <extended-alpha-char>
88 <digit-char> ::= "0" | <nonzero-digit-char>
90 <nonzero-digit-char> ::= "1" | "2" $| \cdots |$ "9"
93 The precise definition of @<alpha-char> is left to the function
94 \textsf{alpha-char-p} in the hosting Lisp system. For portability,
95 programmers are encouraged to limit themselves to the standard ASCII letters.
97 There are no reserved words at the lexical level, but the higher-level syntax
98 recognizes certain identifiers as \emph{keywords} in some contexts. There is
99 also an ambiguity (inherited from C) in the declaration syntax which is
100 settled by distinguishing type names from other identifiers at a lexical
103 \subsubsection{String and character literals} \label{sec:syntax.lex.string}
106 <string-literal> ::= "\"" @<string-literal-char>^* "\""
108 <char-literal> ::= "'" <char-literal-char> "'"
110 <string-literal-char> ::= any character other than "\\" or "\""
113 <char-literal-char> ::= any character other than "\\" or "'"
116 <char> ::= any single character
119 The syntax for string and character literals differs from~C. In particular,
120 escape sequences such as @`\textbackslash n' are not recognized. The use
121 of string and character literals in Sod, outside of C~fragments, is limited,
122 and the simple syntax seems adequate. For the sake of future compatibility,
123 the use of character sequences which resemble C escape sequences is
126 \subsubsection{Integer literals} \label{sec:syntax.lex.int}
129 <integer-literal> ::= <decimal-integer>
130 \alt <binary-integer>
134 <decimal-integer> ::= <nonzero-digit-char> @<digit-char>^*
136 <binary-integer> ::= "0" @("b"|"B"@) @<binary-digit-char>^+
138 <binary-digit-char> ::= "0" | "1"
140 <octal-integer> ::= "0" @["o"|"O"@] @<octal-digit-char>^+
142 <octal-digit-char> ::= "0" | "1" $| \cdots |$ "7"
144 <hex-integer> ::= "0" @("x"|"X"@) @<hex-digit-char>^+
146 <hex-digit-char> ::= <digit-char>
147 \alt "A" | "B" | "C" | "D" | "E" | "F"
148 \alt "a" | "b" | "c" | "d" | "e" | "f"
151 Sod understands only integers, not floating-point numbers; its integer syntax
152 goes slightly beyond C in allowing a @`0o' prefix for octal and @`0b' for
153 binary. However, length and signedness indicators are not permitted.
155 \subsubsection{Punctuation} \label{sec:syntax.lex.punct}
158 <punctuation> ::= any nonalphanumeric character other than "_", "\"" or "'"
161 \subsubsection{Comments} \label{sec:lex-comment}
164 <comment> ::= <block-comment>
169 @<not-star>^* @(@<star>^+ <not-star-or-slash> @<not-star>^*@)^*
175 <not-star> ::= any character other than "*"
177 <not-star-or-slash> ::= any character other than "*" or "/"
179 <line-comment> ::= "//" @<not-newline>^* <newline>
181 <newline> ::= a newline character
183 <not-newline> ::= any character other than newline
186 Comments are exactly as in C99: both traditional block comments `\texttt{/*}
187 \dots\ \texttt{*/}' and \Cplusplus-style `\texttt{//} \dots' comments are
188 permitted and ignored.
190 \subsection{Special nonterminals}
191 \label{sec:special-nonterminals}
193 Aside from the lexical syntax presented above (\xref{sec:lexical-syntax}),
194 two special nonterminals occur in the module syntax.
196 \subsubsection{S-expressions} \label{sec:syntax-sexp}
199 <s-expression> ::= an S-expression, as parsed by the Lisp reader
202 When an S-expression is expected, the Sod parser simply calls the host Lisp
203 system's \textsf{read} function. Sod modules are permitted to modify the
204 read table to extend the S-expression syntax.
206 S-expressions are self-delimiting, so no end-marker is needed.
208 \subsubsection{C fragments} \label{sec:syntax.lex.cfrag}
211 <c-fragment> ::= a sequence of C tokens, with matching brackets
214 Sequences of C code are simply stored and written to the output unchanged
215 during translation. They are read using a simple scanner which nonetheless
216 understands C comments and string and character literals.
218 A C fragment is terminated by one of a small number of delimiter characters
219 determined by the immediately surrounding context -- usually a closing brace
220 or bracket. The first such delimiter character which is not enclosed in
221 brackets, braces or parenthesis ends the fragment.
223 \subsection{Module syntax} \label{sec:syntax-module}
226 <module> ::= @<definition>^*
228 <definition> ::= <import-definition>
229 \alt <load-definition>
230 \alt <lisp-definition>
231 \alt <code-definition>
232 \alt <typename-definition>
233 \alt <class-definition>
236 A module is the top-level syntactic item. A module consists of a sequence of
239 \subsection{Simple definitions} \label{sec:syntax.defs}
241 \subsubsection{Importing modules} \label{sec:syntax.defs.import}
244 <import-definition> ::= "import" <string> ";"
247 The module named @<string> is processed and its definitions made available.
249 A search is made for a module source file as follows.
251 \item The module name @<string> is converted into a filename by appending
252 @`.sod', if it has no extension already.\footnote{%
253 Technically, what happens is \textsf{(merge-pathnames name (make-pathname
254 :type "SOD" :case :common))}, so exactly what this means varies
255 according to the host system.} %
256 \item The file is looked for relative to the directory containing the
258 \item If that fails, then the file is looked for in each directory on the
259 module search path in turn.
260 \item If the file still isn't found, an error is reported and the import
263 At this point, if the file has previously been imported, nothing further
265 This check is done using \textsf{truename}, so it should see through simple
266 tricks like symbolic links. However, it may be confused by fancy things
267 like bind mounts and so on.} %
269 Recursive imports, either direct or indirect, are an error.
271 \subsubsection{Loading extensions} \label{sec:syntax.defs.load}
274 <load-definition> ::= "load" <string> ";"
277 The Lisp file named @<string> is loaded and evaluated.
279 A search is made for a Lisp source file as follows.
281 \item The name @<string> is converted into a filename by appending @`.lisp',
282 if it has no extension already.\footnote{%
283 Technically, what happens is \textsf{(merge-pathnames name (make-pathname
284 :type "LISP" :case :common))}, so exactly what this means varies
285 according to the host system.} %
286 \item A search is then made in the same manner as for module imports
287 (\xref{sec:syntax-module}).
289 If the file is found, it is loaded using the host Lisp's \textsf{load}
292 Note that Sod doesn't attempt to compile Lisp files, or even to look for
293 existing compiled files. The right way to package a substantial extension to
294 the Sod translator is to provide the extension as a standard ASDF system (or
295 similar) and leave a dropping @"foo-extension.lisp" in the module path saying
298 \textsf{(asdf:load-system :foo-extension)}
300 which will arrange for the extension to be compiled if necessary.
302 (This approach means that the language doesn't need to depend on any
303 particular system definition facility. It's bad enough already that it
304 depends on Common Lisp.)
306 \subsubsection{Lisp escapes} \label{sec:syntax.defs.lisp}
309 <lisp-definition> ::= "lisp" <s-expression> ";"
312 The @<s-expression> is evaluated immediately. It can do anything it likes.
314 \textbf{Warning!} This means that hostile Sod modules are a security hazard.
315 Lisp code can read and write files, start other programs, and make network
316 connections. Don't install Sod modules from sources that you don't
318 Presumably you were going to run the corresponding code at some point, so
319 this isn't as unusually scary as it sounds. But please be careful.} %
321 \subsubsection{Declaring type names} \label{sec:syntax.defs.typename}
324 <typename-definition> ::=
325 "typename" <identifier-list> ";"
328 Each @<identifier> is declared as naming a C type. This is important because
329 the C type syntax -- which Sod uses -- is ambiguous, and disambiguation is
330 done by distinguishing type names from other identifiers.
332 Don't declare class names using @"typename"; use @"class" forward
333 declarations instead.
335 \subsection{Literal code} \label{sec:syntax-code}
338 <code-definition> ::=
339 "code" <identifier> ":" <identifier> @[<constraints>@]
342 <constraints> ::= "[" <constraint-list> "]"
344 <constraint> ::= @<identifier>^+
347 The @<c-fragment> will be output unchanged to one of the output files.
349 The first @<identifier> is the symbolic name of an output file. Predefined
350 output file names are @"c" and @"h", which are the implementation code and
351 header file respectively; other output files can be defined by extensions.
353 The second @<identifier> provides a name for the output item. Several C
354 fragments can have the same name: they will be concatenated together in the
355 order in which they were encountered.
357 The @<constraints> provide a means for specifying where in the output file
358 the output item should appear. (Note the two kinds of square brackets shown
359 in the syntax: square brackets must appear around the constraints if they are
360 present, but that they may be omitted.) Each comma-separated @<constraint>
361 is a sequence of identifiers naming output items, and indicates that the
362 output items must appear in the order given -- though the translator is free
363 to insert additional items in between them. (The particular output items
364 needn't be defined already -- indeed, they needn't be defined ever.)
366 There is a predefined output item @"includes" in both the @"c" and @"h"
367 output files which is a suitable place for inserting @"\#include"
368 preprocessor directives in order to declare types and functions for use
369 elsewhere in the generated output files.
371 \subsection{Property sets} \label{sec:syntax.propset}
374 <properties> ::= "[" <property-list> "]"
376 <property> ::= <identifier> "=" <expression>
379 Property sets are a means for associating miscellaneous information with
380 classes and related items. By using property sets, additional information
381 can be passed to extensions without the need to introduce idiosyncratic
384 A property has a name, given as an @<identifier>, and a value computed by
385 evaluating an @<expression>. The value can be one of a number of types,
386 though the only operators currently defined act on integer values only.
388 \subsubsection{The expression evaluator} \label{sec:syntax.propset.expr}
391 <expression> ::= <term> | <expression> "+" <term> | <expression> "-" <term>
393 <term> ::= <factor> | <term> "*" <factor> | <term> "/" <factor>
395 <factor> ::= <primary> | "+" <factor> | "-" <factor>
398 <integer-literal> | <string-literal> | <char-literal> | <identifier>
399 \alt "?" <s-expression>
400 \alt "(" <expression> ")"
403 The arithmetic expression syntax is simple and standard; there are currently
404 no bitwise, logical, or comparison operators.
406 A @<primary> expression may be a literal or an identifier. Note that
407 identifiers stand for themselves: they \emph{do not} denote values. For more
408 fancy expressions, the syntax
412 causes the @<s-expression> to be evaluated using the Lisp \textsf{eval}
414 %%% FIXME crossref to extension docs
416 \subsection{C types} \label{sec:syntax.c-types}
418 Sod's syntax for C types closely mirrors the standard C syntax. A C type has
419 two parts: a sequence of @<declaration-specifier>s and a @<declarator>. In
420 Sod, a type must contain at least one @<declaration-specifier> (i.e.,
421 `implicit @"int"' is forbidden), and storage-class specifiers are not
424 \subsubsection{Declaration specifiers} \label{sec:syntax.c-types.declspec}
427 <declaration-specifier> ::= <type-name>
428 \alt "struct" <identifier> | "union" <identifier> | "enum" <identifier>
429 \alt "void" | "char" | "int" | "float" | "double"
430 \alt "short" | "long"
431 \alt "signed" | "unsigned"
434 <qualifier> ::= "const" | "volatile" | "restrict"
436 <type-name> ::= <identifier>
439 A @<type-name> is an identifier which has been declared as being a type name,
440 using the @"typename" or @"class" definitions.
442 Declaration specifiers may appear in any order. However, not all
443 combinations are permitted. A declaration specifier must consist of zero or
444 more @<qualifiers>, and one of the following, up to reordering.
447 \item @"struct" @<identifier>, @"union" @<identifier>, @"enum" @<identifier>
449 \item @"char", @"unsigned char", @"signed char"
450 \item @"short", @"unsigned short", @"signed short"
451 \item @"short int", @"unsigned short int", @"signed short int"
452 \item @"int", @"unsigned int", @"signed int", @"unsigned", @"signed"
453 \item @"long", @"unsigned long", @"signed long"
454 \item @"long int", @"unsigned long int", @"signed long int"
455 \item @"long long", @"unsigned long long", @"signed long long"
456 \item @"long long int", @"unsigned long long int", @"signed long long int"
457 \item @"float", @"double", @"long double"
459 All of these have their usual C meanings.
461 \subsubsection{Declarators} \label{sec:syntax.c-types.declarator}
464 <declarator>$[k]$ ::= @<pointer>^* <primary-declarator>$[k]$
466 <primary-declarator>$[k]$ ::= $k$
467 \alt "(" <primary-declarator>$[k]$ ")"
468 \alt <primary-declarator>$[k]$ @<declarator-suffix>^*
470 <pointer> ::= "*" @<qualifier>^*
472 <declarator-suffix> ::= "[" <c-fragment> "]"
473 \alt "(" <arguments> ")"
475 <arguments> ::= $\epsilon$ | "..."
476 \alt <argument-list> @["," "..."@]
478 <argument> ::= @<declaration-specifier>^+ <argument-declarator>
480 <argument-declarator> ::= <declarator>@[<identifier> @! $\epsilon$@]
482 <simple-declarator> ::= <declarator>@[<identifier>@]
484 <dotted-name> ::= <identifier> "." <identifier>
486 <dotted-declarator> ::= <declarator>@[<dotted-name>@]
489 The declarator syntax is taken from C, but with some differences.
491 \item Array dimensions are uninterpreted @<c-fragments>, terminated by a
492 closing square bracket. This allows array dimensions to contain arbitrary
493 constant expressions.
494 \item A declarator may have either a single @<identifier> at its centre or a
495 pair of @<identifier>s separated by a @`.'; this is used to refer to
496 slots or messages defined in superclasses.
498 The remaining differences are (I hope) a matter of presentation rather than
501 \subsection{Defining classes} \label{sec:syntax.class}
504 <class-definition> ::= <class-forward-declaration>
505 \alt <full-class-definition>
508 \subsubsection{Forward declarations} \label{sec:class.class.forward}
511 <class-forward-declaration> ::= "class" <identifier> ";"
514 A @<class-forward-declaration> informs Sod that an @<identifier> will be used
515 to name a class which is currently undefined. Forward declarations are
516 necessary in order to resolve certain kinds of circularity. For example,
520 class Super : SodObject {
529 \subsubsection{Full class definitions} \label{sec:class.class.full}
532 <full-class-definition> ::=
534 "class" <identifier> ":" <identifier-list>
535 "{" @<class-item>^* "}"
537 <class-item> ::= <slot-item> ";"
540 \alt <initializer-item> ";"
543 A full class definition provides a complete description of a class.
545 The first @<identifier> gives the name of the class. It is an error to
546 give the name of an existing class (other than a forward-referenced class),
547 or an existing type name. It is conventional to give classes `MixedCase'
548 names, to distinguish them from other kinds of identifiers.
550 The @<identifier-list> names the direct superclasses for the new class. It
551 is an error if any of these @<identifier>s does not name a defined class.
553 The @<properties> provide additional information. The standard class
554 properties are as follows.
556 \item[@"lisp_class"] The name of the Lisp class to use within the translator
557 to represent this class. The property value must be an identifier; the
558 default is @"sod_class". Extensions may define classes with additional
559 behaviour, and may recognize additional class properties.
560 \item[@"metaclass"] The name of the Sod metaclass for this class. In the
561 generated code, a class is itself an instance of another class -- its
562 \emph{metaclass}. The metaclass defines which slots the class will have,
563 which messages it will respond to, and what its behaviour will be when it
564 receives them. The property value must be an identifier naming a defined
565 subclass of @"SodClass". The default metaclass is @"SodClass".
566 %%% FIXME xref to theory
567 \item[@"nick"] A nickname for the class, to be used to distinguish it from
568 other classes in various limited contexts. The property value must be an
569 identifier; the default is constructed by forcing the class name to
573 The class body consists of a sequence of @<class-item>s enclosed in braces.
574 These items are discussed on the following sections.
576 \subsubsection{Slot items} \label{sec:sntax.class.slot}
581 @<declaration-specifier>^+ <init-declarator-list>
583 <init-declarator> ::= <declarator> @["=" <initializer>@]
586 A @<slot-item> defines one or more slots. All instances of the class and any
587 subclass will contain these slot, with the names and types given by the
588 @<declaration-specifiers> and the @<declarators>. Slot declarators may not
589 contain qualified identifiers.
591 It is not possible to declare a slot with function type: such an item is
592 interpreted as being a @<message-item> or @<method-item>. Pointers to
595 An @<initializer>, if present, is treated as if a separate
596 @<initializer-item> containing the slot name and initializer were present.
600 class Example : Super {
607 class Example : Super {
613 \subsubsection{Initializer items} \label{sec:syntax.class.init}
616 <initializer-item> ::= @["class"@] <slot-initializer-list>
618 <slot-initializer> ::= <qualified-identifier> "=" <initializer>
620 <initializer> :: "{" <c-fragment> "}" | <c-fragment>
623 An @<initializer-item> provides an initial value for one or more slots. If
624 prefixed by @"class", then the initial values are for class slots (i.e.,
625 slots of the class object itself); otherwise they are for instance slots.
627 The first component of the @<qualified-identifier> must be the nickname of
628 one of the class's superclasses (including itself); the second must be the
629 name of a slot defined in that superclass.
631 The initializer has one of two forms.
633 \item A @<c-fragment> enclosed in braces denotes an aggregate initializer.
634 This is suitable for initializing structure, union or array slots.
635 \item A @<c-fragment> \emph{not} beginning with an open brace is a `bare'
636 initializer, and continues until the next @`,' or @`;' which is not within
637 nested brackets. Bare initializers are suitable for initializing scalar
638 slots, such as pointers or integers, and strings.
641 \subsubsection{Message items} \label{sec:syntax.class.message}
646 @<declaration-specifier>^+ <declarator> @[<method-body>@]
649 \subsubsection{Method items} \label{sec:syntax.class.method}
654 @<declaration-specifier>^+ <declarator> <method-body>
656 <method-body> ::= "{" <c-fragment> "}" | "extern" ";"
660 %%%----- That's all, folks --------------------------------------------------
664 %%% TeX-master: "sod.tex"