ERights Home elang / grammar 
Back to: Expression Grammar x On to: Pattern Grammar

Primitive
Expressions


The primitive expressions have no parsing ambiguities that need to be resolved by precedence and associativity. Instead, they form the "atomic" expressions that the rest of the expression grammar builds on.

Parenthesized Expression

Grammar Meaning Expansion
"(" expr ")"

disambiguates grouping. Does not introduce a new scope.

N/A

Although "a + b * c + d" parses as "a + (b * c) + d", you can obtain a different ordering by saying, for example, "(a + b) * (c + d)".

varName: Variable-Name Expression

Grammar Example Meaning Expansion
Identifier
foo

The value of that variable

kernel
"_"
_

meta variable for making regions and twisters

ForAllX

A variable name as an expression yields the value of that variable. Which variable? The variable of the same name whose definition is in scope. The scope rules are explained here. The syntax of an identifier is the same as in Java, except that the "$" character isn't allowed, and names beginning with "_" are reserved for compiler generated temporary variable names.

When the keyword "_" is used as an expression, it is just shorthand for the variable name "ForAllX". It is best thought of as a blank to be filled in, making a region or a twister accepting candidates for that blank. For example, "def r := _ >= 3" produces a region containing all integers greater than or equal to three. Such a region can be used as a one argument function, and acts as if that argument fills in the blank. For example, "r(5)" yields true, as if we were evaluating "5 >= 3".

Literal Expression

Grammar Example Meaning Expansion
LiteralInteger
LiteralFloat64
LiteralCharacter
LiteralString
37
3.14159
'c'
"hello literal"

The described value

kernel

LiteralIntegers are precision-unlimited integers expressed as a sequence of digits in base 10, 8, or 16, depending on whether it begins with a non-zero digit, "0", or "0x" respectively. The syntax is just like Java's except that it may also contain embedded "_"s which are ignored. This lets you write more readable large integers like 31_536_000 (the number of seconds in a non-leap-year).

LiteralFloat64s are also just like Java's literal double precision numbers, except that E also allows and ignores embedded "_"s.

LiteralCharacters are just like Java's literal unicode characters.

LiteralStrings are just like Java literal unicode strings.

Quasi-Literal Expression

Grammar Meaning Expansion
parserName`quasi`

Almost literal expression in an embedded grammar

quasiParsers["parserName"] \
      valueMaker("quasiString") \
      substitute([arg,..])
`quasi`

Defaults to the "simple" quasi-parser

simple`quasi`

An expression describes both a computation to be executed, and the value yielded by executing that computation. Of these two, the notation of a normal expression is optimized more to make the nature of the computation clear. Since a literal expression involves no computation, its notation is instead optimized to describe the value of the expression. A quasi-literal expression is one that does involve computation to produce a value, and may produce a different value different times, but whose notation is optimized to express the nature of the value produced rather than the computation used to produce it.

To support quasi-literal expressions and quasi-literal patterns (explained elsewhere***), E introduces the notion of the pluggable parser. ***more to be said.

URI-Literal Expression

Grammar Example Meaning Expansion
URILiteral
<file:/autoexec.bat>
c:/autoexec.bat

Retrieve using the URI protocol handler

uriGetters["file"]["/autoexec.bat"]
file:c:/autoexec.bat

Looks up the named protocol handler (the identifier to the left of the colon) in the collection named "uriGetters" in the current scope, and asks that to dereference the uri-body string (the string to the right of the colon). Note that this string may contain a uri fragment specification -- a "#" followed by more uri-body string. It is up to the protocol handler to either interpret this as a fragment, or, if inappropriate, throw an exception. In no case should the protocol handler interpret a "#" and the following string as part of the main uri-body string. Each protocol handler should document how it handles fragments.

If a single letter, "a" thru "z" or "A" thru "Z" appears as a protocol handler, it is assumed to be a drive letter, so an implicit "file:" is added to the left, making the drive letter and the following colon the beginning of the uri-body string.

See "URI Expressions".

List Expression

Grammar Example Meaning Expansion
"[" "]"
"["expr,..."]"
[]
[1, 2, 4, 9]
An empty ConstList
A ConstList of these values
ListMaker()
ListMaker(expr,...)

See ConstList.

Map Expression

Grammar Meaning Expansion
"["(k "=>" v),..."]"

A ConstMap of these pairs (where k is a key expr and v is a value expr

MapMaker([[k,v],...])

See ConstMap.

Nested Block

Grammar Meaning Expansion
{
    nested-scope-expr
}

All definitions within the curlies go out of scope at the close curly

kernel

 

The if Expressions

Grammar Meaning Expansion
if (cond-expr) {
    then-expr
} else {
    else-expr
}

 

kernel
if (cond-expr) {
    then-expr
}
 
if (cond-expr) {
    then-expr
} else {
    null
}
if (cond-expr) {
    then-expr
} else if ...
 
if (cond-expr) {
    then-expr
} else {
    if ...
}

See "The if Expressions".

The switch Expression

Grammar Meaning Expansion
switch (expr0) {
    match patt1 {
        expr1
    }
    ...
}

whichever pattern matches first, execute that body expression

{
    def t := expr0
    if (t =~ patt1) {
        expr1
    } else if ...
        ...
    } else {
        throw(***)
    }
}

 

The try Expressions

Grammar Meaning Expansion
try {
    expr0
} catch patt1 {
    expr1
}

if expr0 throws a matching problem, do expr1 instead

kernel
try {
    expr0
} catch patt1 {
    expr1
} catch patt2 {
    expr2
} ...

do the first matching catch clause

try {
    expr0
} catch prob {
    if (prob =~ patt1) {
        expr1
    } else if (prob =~ patt2) {
        expr2
    } else if ...
        ...
    } else {
        throw(prob)
    }
}
try {
    expr0
} finally {
    expr1
}
do expr1 on the way out
kernel
try {
    expr0
} catch patt1 {
    expr1
} finally {
    expr2
}
When combined, catch nests within finally
try {
    try {
        expr0
    } catch patt1 {
        expr1
    }
} finally {
    expr2
}
try {
    expr0
} catch patt1 {
    expr1
} catch ...
    ...
} finally {
    exprn
}
Combined form also allows multiple catches
try {
    try {
        expr0
    } catch patt1 {
        expr1
    } catch ...
        ...
    }
} finally {
    exprn
}

 

The escape Expression

Grammar Meaning Expansion
escape hatch {
    expr
}
If hatch is called during expr, complete with hatch's argument
kernel

 

 

The while Loop

Grammar Meaning Expansion
while (cond-expr) {
    expr
}
While the condition is true, do the body
escape break {
    loop(thunk {
        escape continue {
            if (cond) {
                expr
            } else {
                break()
            }
        }
    }
}

 

The for Loops

Grammar Meaning Expansion
for vpatt in cexpr {
    bexpr
}
Do expr repeatedly
for _ => vpatt in cexpr {
    bexpr
}
for kpatt => vpatt in cexpr {
    bexpr
}
Do expr until hatch is called
escape break {

    def func(k,v) {
        escape continue {
            if (k =~ kpatt && \
                v =~ vpatt) {
                bexpr
            }
        }
    }
    cexpr iterate(func)
}

 

Defining Objects

Grammar Meaning Expansion
def name(patterns) {
    expr
}

function definition

def name {
    to run(patterns) {
        expr
    }
}
def name {
    method*
}
A purely methodical object
kernel
def name {
    method*
    matcher
}
A partially methodical object
kernel
def name {
    method*
    matcher*
}
extended matcher clause
***
def name match pattern {
    expr
}
A non-methodical object
kernel
class name(patterns) {
    expr
}
Inheritance by delegation
***
 
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
ERights Home elang / grammar 
Back to: Expression Grammar x On to: Pattern Grammar
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign