Skip to content

Instantly share code, notes, and snippets.

@iacore
Created January 17, 2022 16:40
Show Gist options
  • Save iacore/844a1c82aaed3fb5dd7a2200f3fed2c1 to your computer and use it in GitHub Desktop.
Save iacore/844a1c82aaed3fb5dd7a2200f3fed2c1 to your computer and use it in GitHub Desktop.
Koka Lex Grammar written in Ohm
Koka {
space := "\x01".."\x00" // disable builtin rules
spaces := ""
program = token*
token = whitespace | lexeme
whitespace = white | newline
newline = "\r"? "\n"
white = " " | blockcomment | linecomment | linedirective
linecomment = "//" linechar*
linechar = graphic | utf8 | " " | "\t"
blockcomment = "/*" blockchars (blockcomment blockchars)? "*/"
blockchars = (linechar | newline)*
linedirective = newline "#" linechar*
graphic = "\x21".."\x7E"
utf8 = "\u{80}".."\u{10FFFF}" // bug
lexeme = id | wildcard | literal | keyword | keyop | special
id = varid | conid | opid
modulepath = (lowerid "/")+
wildcard = "_" alphanumeric*
varid = lowerid
conid = upperid
opid = "(" op ")"
lowerid = lower alphanumeric*
upperid = upper alphanumeric*
alphanumeric = digits | upper | lower | "-" | "_"
lower := "a" .. "z"
upper := "A".."Z"
digits = "0".."9"
literal = "\"" strchar* "\""
strchar = graphic | utf8 // how do I exclude double quote here?
keyop = "=" | "." | ":" | "->"
keyword = "infix" | "infixr" | "infixl" | "module" | "import" | "as" | "pub" | "abstract" | "type" | "struct" | "alias" | "effect" | "con" | "forall" | "exists" | "some" | "fun" | "fn" | "val" | "var" | "extern" | "if" | "then" | "else" | "elif" | "match" | "return" | "with" | "in" | "handle" | "handler" | "mask" | "ctl" | "final" | "raw" | "override" | "named" | "interface" | "break" | "continue" | "unsafe"
special = "{" | "}" | "(" | ")" | "[" | "]" | ";" | ","
op = symbol+
symbol = anglebar | "$" | "%" | "&" | "*" | "+" | "~" | "!" | "\\" | "^" | "#" | "=" | "." | ":" | "~" | "?"
anglebar = "<" | ">" | "| "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment