Paste: peg.lua

Author: hsuh
Mode: factor
Date: Fri, 4 Dec 2009 23:57:02
Plain Text |
! Copyright (C) 2009 Hugo Schmitt.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays strings math.parser sequences
peg peg.ebnf peg.parsers memoize namespaces math peg.lua.ast ;
IN: peg.lua

#! http://www.lua.org/manual/5.1/manual.html#8

EBNF: parse-lua
Name        = (([a-zA-Z0-9_])+)   => [[ >string ]]

digit       = ([0-9])         => [[ digit> ]]

Number      = (digit)+        => [[ 10 digits>integer ]]

String      = Name            => [[ ast-string boa ]] 

block       = { stat (";")? }* { laststat (";")? }?
                                   
stat        = { varlist "=" explist }             
            | functioncall
            | { "do" block "end" }
            | { "while" exp "do" block "end" }
            | { "repeat" block "until" exp }
            | { "if" exp "then" block { "elseif" exp "then" block }* { "else" block }? "end" }
            | { "for" Name "=" exp "," exp { "," exp }? "do" block "end" }
            | { "for" namelist "in" explist "do" block "end" }
            | { "function" funcname funcbody }
            | { "local" "function" Name funcbody } 
            | { "local" namelist { "=" explist }? }

laststat  = { "return" (explist)? }
            | "break"

funcname    = Name { "." Name }* { ":" Name }?

varlist     = var { "," var }*

var         = { prefixexp "." Name }
            | { prefixexp "[" exp "]" } 
            | Name                               

namelist    = Name { "," Name }*

explist     = { exp "," }* exp

exp         = { { exp binop exp }
              | { unop exp }
              | "nil"
              | "false" 
              | "true" 
              | Number
              | String
              | "..."
              | function
              | prefixexp
              | tableconstructor }

prefixexp   = var | functioncall | { "(" exp ")" }

functioncall = prefixexp args | prefixexp ":" Name args  

args        = { "(" (explist)? ")" }
            | tableconstructor
            | String               
        
function    = "function" funcbody     

funcbody    = "(" (parlist)? ")" block "end"

parlist     = { namelist { "," "..." }? }
            | "..."

tableconstructor = "{" { fieldlist }? "}"

fieldlist   = field { fieldsep field }* { fieldsep }?

field       =  { { "[" exp "]" "=" exp } 
            | { Name "=" exp }
            | exp }
                                
fieldsep    = { "," | ";" }

binop       = { "+" | "-" | "*" | "/" | "^" | "%" | ".." |
                "<" | "<=" | ">" | ">=" | "==" | "~=" | "and" | "or" }
          
unop        = { "-" | "not" | "#" }

Program     = block
;EBNF

New Annotation

Summary:
Author:
Mode:
Body: