Skip to content

文法

Emela の構文を EBNF で書いたものです。仕様の決定を EBNF に落とした初版で、formatter の実装で曖昧さを洗い出し、適合テストとともに確定させていきます。

字句の細かな規則(名前のクラス、エスケープ、行の継続)は 字句と予約語 にあります。

  • { x } は0回以上の繰り返し、[ x ] は省略可能、| は選択を表します。引用符で囲んだものは終端記号です。
  • NL は、文の区切りとして働く改行です(字句と予約語 の行の継続規則)。継続と判定された改行、( [ の内側の改行、効果の集合(uses { ... })の波括弧の内側の改行は、字句解析の段階で取り除かれます。
  • 並びの末尾のカンマはどこでも許されます。規則では省略しています。formatter は、1行に収まる並びでは末尾のカンマを取り、複数行に分けた並びでは付けます。

括弧の役割は種類ごとに決まっています。xs[0] のような添字の構文はありません。

括弧役割
( )値の世界: 関数の呼び出し、構築、パターン、フィールドの宣言、タプル
[ ]型引数とリストリテラル
{ }ブロックと宣言の本体
type Point(
x: Int,
y: Int, # 末尾のカンマ
)
fn main() {
dbg(Point(x: 1, y: 2))
()
}

字句は最長一致で切ります。"""" は """ と " であり、空の文字列 "" を2つ並べたものとは読みません。

lower_name = ( "a".."z" | "_" ) { letter | digit | "_" } (* 予約語を除く *)
type_name = "A".."Z" { letter | digit } (* 小文字を1つ以上含む *)
upper_name = "A".."Z" { "A".."Z" | digit | "_" }
dec_lit = digit { digit | "_" }
int_lit = dec_lit
| "0x" hex_digit { hex_digit | "_" }
| "0b" ( "0" | "1" ) { "0" | "1" | "_" }
float_lit = dec_lit "." dec_lit [ ( "e" | "E" ) [ "+" | "-" ] dec_lit ]
string_lit = '"' { char | escape | "#{" expr "}" } '"'
| '"""' NEWLINE { line } '"""' (* 複数行。インデントの規則は「字句と予約語」 *)
escape = '\\' ( '"' | '\\' | "n" | "t" | "r" | "#" | "u{" hex_digit { hex_digit } "}" )
literal = int_lit | float_lit | string_lit
  • letter と digit は ASCII に限ります。
  • char は "、\、改行を除く文字です。hex_digit は 0〜9、a〜f、A〜F です。
  • 1..2 は dec_lit ".." dec_lit、1.x は dec_lit "." lower_name と読みます。小数点の後に数字がなければ浮動小数点にしません。
module = { import NL } { decl NL }
import = "import" module_path [ "." "{" import_item { "," import_item } "}" ]
module_path = type_name { "." type_name }
import_item = lower_name | type_name | upper_name
decl = { annotation NL } [ "pub" [ "opaque" ] ] item
| impl_decl
item = fn_decl | type_decl | enum_decl | error_decl | const_decl
| effect_decl | handler_decl | layer_decl | trait_decl
annotation = "@" lower_name [ "(" annot_arg { "," annot_arg } ")" ]
annot_arg = lower_name | literal | lower_name ":" literal | qualified
qualified = type_name { "." type_name } "." lower_name
fn_decl = [ "suspend" ] "fn" lower_name [ type_params ]
"(" [ params ] ")" [ signature ] [ block ]
signature = [ "->" type [ "fails" error_set ] ] [ "uses" effect_set ]
params = param { "," param }
param = "self" | lower_name [ ":" type ]
type_params = "[" type_param { "," type_param } "]"
type_param = ( upper_name | type_name ) [ ":" bound { "+" bound } ]
bound = type_ref
type_decl = "type" type_name [ type_params ] [ "(" [ fields ] ")" ] [ derive ]
enum_decl = "enum" type_name [ type_params ] "{" variant { sep variant } "}" [ derive ]
variant = { annotation } type_name [ "(" ( fields | type { "," type } ) ")" ]
error_decl = "error" type_name [ "(" [ fields ] ")" ] [ derive ]
fields = field { "," field }
field = { annotation } lower_name ":" type
derive = "derive" type_ref { "," type_ref }
sep = "," | NL
const_decl = "const" upper_name [ ":" type ] "=" expr
effect_decl = "effect" type_name [ type_params ] "{" { effect_member NL } "}"
| "effect" type_name "of" type_ref [ "[" type { "," type } "]" ] [ "default" ] (* ラベル *)
effect_member = op_sig | member_sig
op_sig = [ "suspend" ] "fn" lower_name [ type_params ] "(" [ params ] ")"
"->" type [ "fails" error_set ] (* type_params は組み込みのエフェクトだけ *)
member_sig = [ "suspend" ] "handler" type_name "(" [ params ] ")" ":" effect_ref { "+" effect_ref }
[ "fails" error_set ] (* handler メンバー *)
handler_decl = "handler" type_name [ "(" [ fields ] ")" ]
":" effect_ref { "+" effect_ref } "{" { handler_item NL } "}"
effect_ref = type_ref [ "[" type { "," type } "]" ]
handler_item = "init" [ "(" [ params ] ")" ] block
| "release" block
| [ "suspend" ] "fn" lower_name "(" [ lower_name { "," lower_name } ] ")" block
| handler_decl (* 内側の handler *)
layer_decl = "layer" type_name "{" supply { sep supply } "}"
supply = type_ref [ "." type_name ] [ "(" [ args ] ")" ]
| lower_name "." type_name "(" [ args ] ")" (* 能力の handler メンバー *)
trait_decl = "trait" type_name [ ":" bound { "+" bound } ] "{" { trait_item NL } "}"
trait_item = "fn" lower_name "(" [ params ] ")" [ signature ] [ block ]
| derive_rule (* 構文は未確定 *)
impl_decl = "impl" type_ref [ type_params ] ":" type_ref "{" { impl_item NL } "}"
impl_item = "fn" lower_name "(" [ params ] ")" block

宣言を一通り並べると、次のようになります。

import Std.Console.{Console, SystemConsole}
error NotFound(id: Int)
enum Shape {
Circle(radius: Float)
Rect(Float, Float)
}
const LIMIT = 10
effect Counter {
fn next() -> Int
}
handler FixedCounter: Counter {
fn next() { LIMIT }
}
layer AppTest {
FixedCounter
SystemConsole
}
pub fn area(s: Shape) -> Float {
s match {
Circle(radius:) -> 3.14 * radius * radius
Rect(w, h) -> w * h
}
}
fn main() {
counter <- Counter
console <- Console
with AppTest {
n = counter.next()
console.print_line("#{n} #{area(Rect(2.0, 3.0))}")
}
}

effect には操作のほかに handler メンバーを宣言できます。handler メンバーは、その effect のどの handler も内側の handler として持つ handler の名前です。with の項目 x = E.M(引数) で開き、x はその handler の能力になります。

error Missing(path: String)
effect Lines {
fn next() -> Option[String]
}
effect Files {
fn exists(path: String) -> Bool
handler Open(path: String): Lines fails Missing
}
handler MemoryFiles: Files {
fn exists(path) { path == "a.txt" }
handler Open(left: Int, path: String): Lines {
init(path: String) {
if path != "a.txt" { fail Missing(path:) }
Self(left: 2, path:)
}
fn next() {
if self.left > 0 { Some(self.path) } else { None }
}
}
}
fn first(lines: use Lines) -> Option[String] {
lines.next()
}
fn main() {
with MemoryFiles {
r = with file = Files.Open(path: "a.txt") {
first(file)
} escape {
Missing(path:) -> None
}
dbg(r)
}
()
}

宣言についての文法の外の制約

Section titled “宣言についての文法の外の制約”
  • import は宣言より前に置きます。宣言より後ろの import は E1201 です(コメントは数えません)。
  • type_decl は型と同じ名前の構成子を1つ定義します。バリアントが1つの enum と同じものです。
  • 1つのバリアントの中で、名前のないフィールドと名前付きのフィールドは混ぜられません(E1202)。
  • 本体のない fn_decl と、トップレベルの suspend fn は、@external が付いている場合にだけ許されます(E1204)。trait_item の fn は既定の実装を持たなくてよく、この制約は掛かりません。
  • fails と uses は文脈依存の語です。uses の位置に use と書くと E1404 で、診断が書き換えを示します。
  • annot_arg の qualified は @entry の引数にだけ書けます。
  • opaque は type と enum にだけ付けられます(E1205)。
  • init の引数は、どれも型を付けた lower_name です。
  • フィールドのない type_decl は、@external(同梱のソースでは @intrinsic)が付いている場合にだけ許されます(E1204)。
  • enum_decl は1つ以上のバリアントを持ちます(E1206)。値を持たない型は Never だけです。
  • type_decl、enum_decl、error_decl の直後の改行に derive が続くとき、その改行は区切りにならず、derive は宣言の続きになります。間の空行とコメントだけの行は飛ばします(行の継続の判定と同じ)。derive は文脈依存の語なので、字句の継続規則には入れず、宣言の位置でだけ扱います。
fn host_now() -> Int # error[E1204]: 本体のない fn には @external が要る
enum Empty {} # バリアントが1つもない
type = type_ref [ "[" type_arg { "," type_arg } "]" ]
| upper_name (* 型引数 *)
(* 型名の型引数は type_ref の形で書き、名前解決で区別する *)
| "Self"
| "(" ")" (* Unit *)
| "(" type ")" (* 括弧 *)
| "(" type "," type { "," type } ")" (* タプル *)
| "fn" "(" [ type { "," type } ] ")" "->" type
[ "fails" error_set ] [ "uses" effect_set ]
| "use" effect_item { "+" effect_item } (* 能力の型 *)
type_ref = [ module_path "." ] type_name
type_arg = type
| "{" [ effect_item { "," effect_item } ] "}" (* 効果の集合。{} は空の集合 *)
| error_item "|" error_item { "|" error_item } (* エラーの集合 *)
error_set = "{" "}" | error_item { "|" error_item }
error_item = type_ref | upper_name (* エラーのタグか、集合の型引数。混ぜてよい *)
effect_set = "{" [ effect_item { "," effect_item } ] "}" | type_ref | upper_name
effect_item = type_ref [ "[" type { "," type } "]" ] | upper_name (* エフェクト(型引数つきでよい)か、集合の型引数。混ぜてよい *)
error Timeout
effect Log {
fn info(msg: String) -> ()
}
type Job[E, R](run: fn() -> Int fails E uses { R })
fn note(log: use Log, msg: String) {
log.info(msg)
}
fn twice[E](f: fn() -> Int fails Timeout | E) -> Int fails Timeout | E {
f() + f()
}
fn start(job: Job[Timeout, { Log }]) -> Int fails Timeout uses { Log } {
job.run()
}
fn pair() -> (Int, fn(Int) -> Int fails {} uses {}) {
(1, |x| x + 1)
}

要素1個のタプルはありません。型の (T) はただの括弧です。

use E は effect E の能力の値の型です。use A + B は複数の effect の能力で、use A + B の能力は、そのうちの一部だけを並べた use A の引数にも渡せます(エフェクト)。

集合の型引数の扱い(どの型引数が集合を受けるか)は文法では決めず、型検査で確かめます(型と効果の体系)。

block = "{" { stmt NL } "}"
stmt = binding | effect_alias | local_fn | expr
binding = pattern [ ":" type ] "=" expr
effect_alias = lower_name "<-" type_ref [ "[" type { "," type } "]" ]
local_fn = "fn" lower_name "(" [ params ] ")" [ signature ] block
expr = "fail" expr
| "assert" expr
| pipe_expr { ( "escape" | "match" ) "{" { arm NL } "}" }
pipe_expr = or_expr { "|>" or_expr }
or_expr = and_expr { "||" and_expr }
and_expr = cmp_expr { "&&" cmp_expr }
cmp_expr = add_expr [ ( "==" | "!=" | "<" | "<=" | ">" | ">=" ) add_expr ]
add_expr = mul_expr { ( "+" | "-" ) mul_expr }
mul_expr = unary { ( "*" | "/" | "%" ) unary }
unary = ( "!" | "-" ) unary
| "use" type_ref [ "[" type { "," type } "]" ] (* 非推奨。W4401 *)
| lambda
| postfix
postfix = primary { "(" [ args ] ")" | "." lower_name | "." type_name "(" [ args ] ")" }
(* "." type_name は handler メンバー。init の中だけ *)
primary = literal | lower_name | upper_name | type_ref | "self" | "Self"
| "(" ")"
| "(" expr ")"
| "(" expr "," expr { "," expr } ")"
| "[" [ expr { "," expr } [ "," ".." expr ] ] "]"
| block | if_expr | with_expr
args = arg { "," arg } [ "," ".." expr ]
| ".." expr
arg = expr | lower_name ":" [ expr ]
if_expr = "if" expr block [ "else" ( block | if_expr ) ]
arm = pattern [ "if" expr ] "->" expr
with_expr = "with" with_item { "," with_item } block
with_item = [ lower_name "=" ] supply [ "by" by_right ] (* by はラベル *)
by_right = type_ref [ "[" type { "," type } "]" | "(" [ args ] ")" ]
lambda = ( "||" | "|" [ lambda_param { "," lambda_param } ] "|" ) expr
lambda_param = pattern [ ":" type ]

cmp_expr は比較演算子を1つしか持てないので、a < b < c は書けません(E1106)。比較のほかの二項演算子と |> は左結合です。escape と match は後置で、続けて何度でも書けます。x.f(a) は postfix の . lower_name と呼び出しの組で、フィールドの関数の呼び出しかメソッド呼び出しかは型検査で決まります(型と効果の体系)。

error NotFound(id: Int)
type User(id: Int, name: String)
fn find(id: Int) -> User fails NotFound {
if id == 1 { User(id: 1, name: "alice") } else { fail NotFound(id:) }
}
fn main() {
## 1を足す。
fn inc(n: Int) -> Int {
n + 1
}
user = find(2) escape {
NotFound(id:) -> User(id:, name: "guest")
}
renamed = User(name: "bob", ..user) # 部分更新
xs = [1, 2, ..[3, 4]]
label = renamed.id match {
n if n > 1 -> "many"
_ -> "one"
}
ys = xs.map(|x| inc(x))
dbg((renamed, ys, label))
()
}
  • 無名関数の本体は expr なので、右へできるだけ長く伸びます。同じ式の中で無名関数の後ろに何かが続くことはありません。呼び出したり演算子を掛けたりするときは括弧で囲みます。
  • fn と lower_name で始まる文はローカルの fn です。式の中の fn( は E1405 で、無名関数は |x| 本体 と書きます。
  • ローカルの fn に型引数を付けると E1213、注釈、pub、suspend を付けると E1212 です。前の行に文書コメント(##)を書けます。
  • 無名関数の引数の並びの中では、括弧の外の | が並びを閉じます。そこに書く関数型の error_set は1項目だけで、2項目以上は ( ) で囲みます。
  • || は、被演算子の位置では引数のない無名関数、被演算子の後では or_expr の演算子です。
  • ブロックの最後の文が式のとき、その値がブロックの値になります。最後が束縛のとき、ブロックの値は () です。最後の文にローカルの fn は置けません(E1214)。
  • 文が束縛か式かは、パターンとして読める並びの直後に = または : があるかで区別します。左辺がパターンとして読めなければ E1111 です。
  • lower_name の直後に <- が続く文は effect_alias(エフェクトの別名、エフェクト)です。
  • 式の use X は非推奨です(W4401)。0.30 でエラーになります。use X の操作をその場で呼ぶときは (use X).op(...) と括弧で囲み、use X の直後に . や ( を書くと E1108 です。
  • 引数の並びでは名前付き引数の後に位置引数を書けません(E1107)。
  • by の右の型引数(Hits by State[Int])は effect を指すときだけ、引数は handler を指すときだけ書けます(ラベル)。
  • 式としての Self は handler の値で、handler の init の中でだけ、フィールドを渡して呼ぶか単独で書けます。
  • 引数の並びの末尾の ..式 は、構成子の部分更新です(データ)。関数の呼び出しに書くと E2111 です。.. の後に引数を書くと E1104 です。部分更新で変えるフィールドは名前付きで書き、位置では書けません(E2112)。
  • 式の (x) はただの括弧で、要素1個のタプルにはなりません。
  • リストのリテラルの ..式 は末尾に1回だけ書けます。[x, y, ..rest] は rest の前に x、y を付けたリストです。.. の後は名前に限らず任意の式でよく([x, ..List.tail(xs)])、要素なしの [..rest](E1104)と、途中や複数の ..([..a, ..b])は書けません。
fn main() {
a = [1]
b = [..a] # error[E1104]: .. の前に要素が要る
}
fn connect(host: String, port: Int) -> String { host }
fn main() {
connect(host: "localhost", 8080) # error[E1107]: 名前付きの後に位置引数
}
pattern = "_"
| lower_name
| literal
| "-" ( int_lit | float_lit ) (* 負の数。パターンだけ *)
| upper_name
| type_ref [ "(" pat_args ")" ]
| "(" ")"
| "(" pattern "," pattern { "," pattern } ")"
| "[" "]"
| "[" pattern { "," pattern } [ "," ".." [ lower_name ] ] "]"
pat_args = ".."
| pat_arg { "," pat_arg } [ "," ".." ]
pat_arg = pattern | lower_name ":" [ pattern ]
const ZERO = 0
type User(id: Int, name: String, admin: Bool)
fn sign(n: Int) -> String {
n match {
ZERO -> "zero"
-1 -> "minus one"
_ -> "other"
}
}
fn describe(xs: List[Int]) -> String {
xs match {
[] -> "empty"
[x] -> "one: #{x}"
[x, ..rest] -> "first #{x}, then #{rest}"
}
}
fn is_admin(u: User) -> Bool {
u match {
User(admin: True, ..) -> True
User(..) -> False
}
}
fn main() {
dbg((sign(-1), describe([1, 2]), is_admin(User(id: 1, name: "a", admin: True))))
()
}
  • パターンの (a) は書けません。要素1個のタプルはありません。
  • 1つのパターンか引数の並びで、同じ名前を2回束縛すると E2109 です。
  • パターンの文字列に補間は書けません(E1105)。
  • パターンの .. は最後に置きます(E1104)。
  • 名前付きフィールドの構成子のパターンで、.. を書かずにフィールドを省くことはできません(E3105)。