Language Grammar

The following BNF Grammar defines the syntax of the Treescript programming language. Non-terminal symbols are written in italics, e.g. while-stmt. Square brackets ([]) enclose something that is optional (can occur 0 or 1 times). Square brackets followed by an ellipsis (...) enclose something that repeats (can occur 0 or more times). A non-terminal symbol followed by an ellipsis indicates repetition (can occur 1 or more times). If a given non-terminal can be replaced by multiple expressions, each one is usually on a separate line. Otherwise, the vertical slash (|) is used to separate them. A backslash (\) indicates that the rest of the line is a comment.

 

source-file:

·      [pkg-stmt][import-stmt]... [priv-cls-def]... pub-cls-def [priv-cls-def]...

 

pkg-stmt:

·      package pkg-list

 

pkg-list:

·      pkg-name

·      ( pkg-name... )

 

pkg-name:

·      identifier

 

import-stmt:

·      import import-list

 

import-list:

·      ( pkg-name... cls-name )

·      ( pkg-name... int-name )

·      ( pkg-name... * )

 

pub-cls-def:

·      pub-class-def

·      pub-interf-def

 

priv-cls-def:

·      priv-class-def

·      priv-interf-def

 

class-def:

·      pub-class-def

·      priv-class-def

 

interf-def:

·      pub-interf-def

·      priv-interf-def

 

pub-class-def:

·      ( class pub-class-modif class-name [extends-clause][implements-clause] class-body )

 

priv-class-def:

·      ( class [priv-class-modif] class-name [extends-clause][implements-clause] class-body )

 

pub-class-modif:

·      ( public abstract )

·      ( public final )

·      ( public )

 

priv-class-modif:

·      ( abstract )

·      ( final )

 

class-name:

·      cls-name

·      ( pkg-name... cls-name )

 

extends-clause:

·      extends class-name

 

pub-interf-def:

·      ( interface ( public ) interf-name [extends-interf-clause] interf-body )

 

priv-interf-def:

·      ( interface interf-name [extends-interf-clause] interf-body )

 

extends-interf-clause:

·      extends ( [interf-name]... )

 

interf-name:

·      int-name

·      ( pkg-name... int-name )

 

class-body:

·      [field-var-list]... [stat-obj-init] method-def...

 

interf-body:

·      [field-var-list]... method-hdr...

 

method-def:

·      proc-def

·      func-def

·      cons-def

·      event-handler

 

proc-def:

·      ( proc [modifier-list] method-name [parm-list] [throws-clause] [loc-var-list]... do-with block )

 

func-def:

·      ( func [modifier-list] type method-name [parm-list] [throws-clause] [loc-var-list]... do-with block )

 

cons-def:

·      ( cons [modifier-list] class-name [parm-list] [throws-clause] [loc-var-list]... do-with cons-block )

 

event-handler:

·      ( proc ( auto ) method-name ( event-class-name identifier ) [throws-clause] [loc-var-list]... do-with block )

 

method-hdr:

·      proc-hdr

·      func-hdr

 

proc-hdr:

·      ( proc [modifier-list] method-name [parm-list] [throws-clause] )

 

func-hdr:

·      ( func [modifier-list] type method-name [parm-list] [throws-clause] )

 

throws-clause:

·      throws exception-list

 

exception-list:

·      exception

·      ( exception... )

 

exception:

·      exception-class-name

 

modifier-list:

·      ( modifier... )

 

modifier:

·      public

·      protected

·      private

·      final

·      static

·      abstract \ ___methods only

·      synchronized \ ___methods only

·      transient \ ___variables only

·      volatile \ ___variables only

 

stat-obj-init:

·      [stat-init] [obj-init]

·      [obj-init] [stat-init]

 

stat-init:

·      static block

 

obj-init:

·      do block

 

parm-list:

·      ( parm [ ; parm]... )

 

parm:

·      type id-list

 

field-var-list:

·      var [modifier-list] ( field-decl [ ; field-decl]... )

 

loc-var-list:

·      var [modifier-list] ( loc-decl [ ; loc-decl]... )

 

field-decl:

·      [modifier-list][property-decl] type id-list [expr]

·      [modifier-list] typedef type type-name

 

loc-decl:

·      [modifier-list] type id-list [expr]

·      [modifier-list] typedef type type-name

 

property-decl:

·      ( property read )

·      ( property write )

·      ( property read write )

 

id-list:

·      identifier

·      ( identifier... )

 

type:

·      simple-type

·      class-name

·      list [type]

·      set set-type

·      array [dim-size] type

·      enum ( identifier... )

 

simple-type:

·      byte

·      short

·      int

·      long

·      float

·      double

·      char

·      boolean

 

set-type:

·      enum-type

·      ( .. const const )

 

dim-size:

·      pos-integer    \ zero-based

·      neg-integer    \ one-based

·      0

 

pos-integer:

neg-integer:

·      integer-constant

 

do-with:

·      do

·      with-do

 

with-do:

·      with with-obj do

 

with-obj:

·      identifier

·      ( identifier… )

 

block:

·      ( [stmt ;]... )

·      ( stmt [ ; stmt]... )

 

stmt:

·      if-stmt

·      switch-stmt

·      while-stmt

·      for-stmt

·      with-stmt

·      assignment-stmt

·      inc-dec-stmt

·      ptr-stmt

·      call-stmt

·      jump-stmt

·      try-stmt

·      throw-stmt

·      synch-stmt

 

if-stmt:

·      if bool-expr then block [elseif-clause]... [ else block]

 

elseif-clause:

·      elseif bool-expr then block

 

switch-stmt:

·      switch expr ( case-clause... [ default block] )

 

case-clause:

·      case expr-list do block

 

while-stmt:

·      while bool-expr do block

·      do block while bool-expr

 

for-stmt:

·      for identifier ( init-expr limit-expr ) [ by step-expr] do block

·      for identifier list-variable [ by step-expr] do block

 

with-stmt:

·      with-do block

 

assignment-stmt:

·      asst-op var-expr expr

 

inc-dec-stmt:

·      ++ var-expr

·      -- var-expr

 

ptr-stmt:

·      pchild var-expr expr

·      pnext var-expr expr

 

call-stmt:

·      proc-name [expr]...

·      : colon-head [colon-expr]… proc-name

·      : colon-head [colon-expr]… ( proc-name expr… )

 

jump-stmt:

·      break

·      continue

·      return [expr]

 

try-stmt:

·      try block [catch-clause]... [ finally block]

 

catch-clause:

·      catch ( exception exception-var ) do block

 

throw-stmt:

·      throw throw-obj

 

synch-stmt:

·      synchronized obj-name do block

 

expr:

·      const-expr

·      var-expr

·      ( op expr... )

·      func-call

·      array-ref

·      obj-expr

·      list-expr

 

var-expr:

·      var-name

·      prop-name

·      array-ref

·      ( : colon-head [colon-expr]… var-end-expr )

 

var-end-expr:

·      prop-name

·      array-ref

 

obj-expr:

·      obj-name

·      ( : colon-head colon-expr… )

 

func-call:

·      func-name

·      ( func-name expr… )

 

array-ref:

·      array-name

·      ( array-name expr… )

 

colon-head:

·      obj-name

·      func-call

·      ( array-name expr… )

 

colon-expr:

·      func-call

·      prop-name

·      class-name

·      array-ref

 

expr-list:

·      expr

·      ( expr... )

 

list-expr:

·      ( list expr... )

 

const-expr:

·      int-const

·      float-const

·      string-const

·      bool-const

·      nil

 

asst-op:

·      = | =+ | =- | =* | =/ | =% | =& | =^ | =<< | =>> | =>>>

·      =|

 

op:

·      + | - | * | / | % | & | ^ | << | >> | >>> | ?

·      |

·      == | <> | > | < | >= | <= | not | and | or | xor | in

·      pchild | pnext | new | resize | instanceof

 

bool-const:

·      true

·      false

 

cls-name:

int-name:

method-name:

proc-name:

func-name:

obj-name:

prop-name:

array-name:

type-name:

list-variable:

exception-class-name:

·      identifier

 

identifier: \ ___no white space allowed between tokens

·      first-char [id-char]…

 

first-char:

·      underscore

·      letter

 

id-char:

·      underscore

·      letter

·      digit

·      hyphen

 

alphanumeric:

·      digit

·      letter

 

digit:

·      0 | 1 | ... | 9

 

letter:

·      A | B | ... | Z

·      a | b | ... | z

 

underscore:

·      _

 

hyphen: \ ___must be immediately preceded and succeeded by alphanumeric

·      -