Skip to content

Instantly share code, notes, and snippets.

@jasonjckn
Created January 8, 2012 04:37
Show Gist options
  • Save jasonjckn/1577224 to your computer and use it in GitHub Desktop.
Save jasonjckn/1577224 to your computer and use it in GitHub Desktop.
(def identifier (m-re #"[a-zA-Z][a-zA-Z0-9]*")) ;; TODO: Is this accurate?
(def modifier (one-of-symb "public protected private static abstract final native
sychronized transient volatile strictfp"))
(def infix-op (one-of-symb "|| && | ^ & == = < > <= << >> > - + * / %"))
(def prefix-op (one-of-symb "++ -- ! ~ + -"))
(def postfix-op (one-of-symb "++ --"))
(def basic-type (one-of-symb "byte short char int long float double boolean"))
(def void (symb "void"))
(def bool-literal (one-of-symb "true false"))
(def literal (<|> stringLiteral
(one-of-symb "null")
bool-literal
natural)) ;; TODO
;; TODO: incomplete
(def simple-expr
(many
(<|> (<$> vec (parens (lazy simple-expr)))
literal
identifier
infix-op)))
(def assignment (m-assoc* :syntax :assignment
:lhs identifier ;; TODO: incomplete??
_ (symb "=")
:rhs simple-expr))
(def expr (<|> assignment simple-expr)) ;; TODO: implement expressions.
(def statement) ;; Forward declare.
;; TODO: Is this right?
(def block (braces (<|> (many (lazy statement)))))
(def if-else (m-assoc* :syntax :if-else
_ (symb "if")
:cond (parens expr)
:when-true statement)) ;; TODO
(def return (m-assoc* :syntax :return
_ (symb "return")
:expr expr
_ semi))
(def expr-statement (<* expr semi))
(def statement (<|> block if-else return expr-statement)) ;; TODO correct???
(def type basic-type) ;; TODO: Not complete.
(def var-decl (m-assoc* :type type
:id identifier))
(def parameter var-decl) ;; TODO: not actually var-decl
(def method-decl (m-assoc* :syntax :method-decl
:modifier modifier ;; TODO: what about multiple modifiers?
:type (<|> type void)
:id identifier
:params (parens (sep-by parameter comma))
;; TODO: What about throws?
:body block)) ;; TODO: Can body be omitted?
(def field-decl (m-assoc* :syntax :field-decl
:modifier modifier ;; TODO: what about multiple modifiers?
:type type
:id identifier
:init (optional (*> (symb "=") expr))))
;; TODO: class extends implements
;; TODO: body is not complete
(def class-decl (m-assoc* :syntax :class-decl
:modifier modifier
_ (symb "class")
:id identifier
:body (braces (many method-decl))))
@jasonjckn
Copy link
Author

jasonjckn commented Jan 21, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment