lua-parser.lisp 7.5 KB
Newer Older
1 2 3 4 5 6 7
;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE:               lua-parser.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             Common-Lisp
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
8
;;;;
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
9 10 11
;;;;    Defines the LUA 5.2 Parser.
;;;;    http://www.lua.org/manual/5.2/manual.html#9
;;;;
12 13 14 15 16
;;;;AUTHORS
;;;;    <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;;    2012-07-15 <PJB> Created.
;;;;BUGS
17 18 19
;;;;
;;;;    This is unfinished.
;;;;
20 21
;;;;LEGAL
;;;;    AGPL3
22
;;;;
23
;;;;    Copyright Pascal J. Bourguignon 2012 - 2016
24
;;;;
25 26 27 28
;;;;    This program is free software: you can redistribute it and/or modify
;;;;    it under the terms of the GNU Affero General Public License as published by
;;;;    the Free Software Foundation, either version 3 of the License, or
;;;;    (at your option) any later version.
29
;;;;
30 31 32 33
;;;;    This program is distributed in the hope that it will be useful,
;;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;;    GNU Affero General Public License for more details.
34
;;;;
35 36 37
;;;;    You should have received a copy of the GNU Affero General Public License
;;;;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
38 39
(eval-when (:compile-toplevel :load-toplevel :execute)
  (setf *readtable* (copy-readtable nil)))
40
(in-package "COM.INFORMATIMAGO.LANGUAGES.LUA.PARSER")
41

Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
42

43
(defgrammar lua
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  :trace nil
  :scanner lua-scanner
  :terminals ((number "[0-9]+")
              (string "\"[^\"]*\"")
              (name  "[_A-Za-z][_A-Za-z0-9]*"))
  :start chunk
  :rules (

          ;; (--> chunk
          ;;      block)
          (--> chunk
               exp)

          (--> block
               (seq (rep stat) (opt retstat)))

          (--> stat
               (alt ";"
                    (seq varlist "=" explist)
                    (seq functioncall)
                    (seq label)
                    (seq "break")
                    (seq "goto" Name)
                    (seq "do" block "end")
                    (seq "while" exp "do" block "end")
                    (seq "repeat" block "until" exp)
                    (seq "if" exp "then" block (rep "elseif" exp "then" block) (opt "else" block) "end")

                    (seq "for" Name (alt for-name-=
                                         for-namelist))

                    (seq "function" funcname funcbody)
                    (seq "local"
                         (alt (seq "function" Name funcbody)
                              (seq namelist (opt "=" explist))))))

          (--> for-name-=
               (seq "=" exp "," exp (opt "," exp) "do" block "end"))
          (--> for-namelist
               (seq namelist-cont "in" explist "do" block "end"))

          (--> retstat
               (seq "return" (opt explist) (opt ";")))

          (--> label
               (seq "::" Name "::"))

          (--> funcname
               (seq Name (rep (seq "." Name)) (opt (seq ":" Name))))

          (--> varlist
               (seq var (rep (seq "," var))))

          (--> namelist
               (seq Name namelist-cont))

          (--> namelist-cont
               (rep (seq "," Name)))

          (--> explist
               (seq exp (rep (seq "," exp))))

          #-(and)
          (--> exp
               (alt "nil"
                    "false"
                    "true"
                    Number
                    String
                    "..."
                    functiondef
                    prefixexp
                    tableconstructor
                    (seq exp binop exp)
                    (seq unop exp)))

          (--> exp disjonction)
          (--> disjonction
               (seq conjonction (rep "or" conjonction)))
          (--> conjonction
               (seq comparaison (rep "and" comparaison)))
          (--> comparaison
               (seq concatenation (rep (alt "<" ">" "<=" ">=" "~=" "==") concatenation)))
          (--> concatenation
               (seq summation (rep ".." summation)))
          (--> summation
               (seq term (rep (alt "+" "-") term)))
          (--> term
               (seq factor (rep (alt "*" "/" "%") factor)))
          (--> factor
               (seq (opt (alt "not" "#" "-")) exponentiation))
          (--> exponentiation
               (seq simple (rep "^" simple)))
          (--> simple
               (alt
                "nil"
                "false"
                "true"
                Number
                String
                "…"
                functiondef
                prefixexp
                tableconstructor))



          #-(and)
          (--> prefixexp
               (alt var
                    functioncall
                    "(" exp ")"))
          #-(and)
          (--> var
               (alt Name
                    (seq prefixexp "[" exp "]")
                    (seq prefixexp "." Name)) )

          #-(and)
          (--> functioncall
               (alt (seq prefixexp args)
                    (seq prefixexp ":" Name args)))

          #-(and)
          (--> callpart
               (seq (opt (seq ":" name)) args))

          (--> indexpart
               (alt (seq "[" exp "]")
                    (seq "." Name)))

          #-(and)
          (--> prefixexp
               (alt
                (seq (alt Name
                          (seq "(" exp ")"))
                     (rep (alt callpart
                               indexpart)))))
          ;; TODO:
          (--> prefixexp
               "prefix")

          ;; TODO:
          (--> var
               Name)

          ;; TODO:
          (--> functioncall
               (seq "funcall" Name "(" exp ")"))







          (--> args
               (alt (seq "(" (opt explist) ")" )
                    tableconstructor
                    String) )

          (--> functiondef
               "function" funcbody)

          (--> funcbody
               (seq "(" (opt parlist) ")" block "end"))

          (--> parlist
               (alt (seq namelist (opt "," "..."))
                    "..."))

          (--> tableconstructor
               (seq "{" (opt fieldlist) "}"))

          (--> fieldlist
               (seq field (rep fieldsep field) (opt fieldsep)))

          (--> field
               (alt (seq "[" exp "]" "=" exp )
                    (seq Name "=" exp)
                    exp))

          (--> fieldsep
               (alt ","
                    ";"))

          #-(and)
          (--> binop
               (alt "+"
                    "-"
                    "*"
                    "/"
                    "^"
                    "%"
                    ".."

                    "<"
                    "<="
                    ">"
                    ">="
                    "=="
                    "~="
246

247 248
                    "and"
                    "or"))
249

250 251 252 253 254
          #-(and)
          (--> unop
               (alt "-"
                    "not"
                    "#"))))
255 256 257



Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
258
;; Operator precedence in Lua follows the table below, from lower to
259
;; higher priority:
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
260 261 262 263 264 265 266 267 268
;;
;;      or
;;      and
;;      <     >     <=    >=    ~=    ==
;;      ..
;;      +     -
;;      *     /     %
;;      not   #     - (unary)
;;      ^
269
;;
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
270 271 272
;; As usual, you can use parentheses to change the precedences of an
;; expression. The concatenation ('..') and exponentiation ('^')
;; operators are right associative. All other binary operators are left
273
;; associative.
274 275

;;;; THE END ;;;;