Skip to content

Instantly share code, notes, and snippets.

@maxymania
Created November 20, 2015 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxymania/336d1a5b13d9f51c2b7b to your computer and use it in GitHub Desktop.
Save maxymania/336d1a5b13d9f51c2b7b to your computer and use it in GitHub Desktop.
Parser for a C-like language
/*
Copyright (c) 2015 Simon Schmidt
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgement in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
package cparser
import "io"
import "strings"
import "github.com/maxymania/langtransf/syntaxfile"
import "github.com/maxymania/langtransf/parsing"
import "github.com/maxymania/langtransf/ast"
const rules = `
/* BEGIN RULES */
srcfile = (definition* #verbose EOF #mute) #precise;
type = Ident;
forParam =
~("Expr" expr) (","#- ~("Expr" expr))*;
paramlist = ~("PL" "("#-
( ~("P" type Ident) ~("P" ","#- type Ident)* )?
")"#- )
;
definition
= ~("DV" type Ident ";"#- )
| ~("DF" type Ident paramlist ";"#-)
| ~("F" type Ident paramlist stmblock)
;
exprlist
= "("#- ( ~("Px" expr) ~("Px" ","#- expr)* )? ")"#-;
unop = "-"|"~"|"!";
binop
= ~("<<" "<" "<")
| ~(">>" ">" ">")
| ~("and" "&" "&")
| ~("or" "|" "|")
| ~("==" "=" "=")
| "+"|"-"|"*"|"/"|"%"|"<"|">"|"&"| "|" |"^"
;
expr0
= Int
| Float
| Char
| String
| RawString
| ~("Call" Ident exprlist)
| Ident
| "("#- expr ")"#-
| ~("Unop" unop#E expr )
;
expr1
= ~("Op" binop expr0)
| ~("[]" "["#- expr "]"#-)
| ~(".Call" "."#- Ident exprlist)
| ~("." "."#- Ident)
| ~("=" "="#- expr)
;
expr = expr0 expr1*;
condition = ~("Cond" expr);
stmblock = ~("Code" "{"#- statement*#e "}"#E#- )#p;
switchCase
= ~("Case" "case"#- condition ":"#- statement*)
| ~("Default" "default"#- ":"#- statement*)
;
switchBlock = "{"#- switchCase* "}"#-;
statement =(
";"#-#E
| ~("DV" type#-#E Ident ";"#-)
| ~("If" "if"#-#E "("#- condition ")"#- ~("Then" statement) ~("Else" "else"#- statement)? )
| ~("While" "while"#-#E "("#- condition ")"#- statement)
| ~("DoWhile" "do"#-#E statement "while"#- "("#- condition ")"#- ";"#- )
| ~("While" "for"#-#E "("#- ~("Pre" forParam)? ";"#- condition? ";"#- ~("Post" forParam)?
")"#- statement )
| ~("Switch" "switch"#-#E "("#- condition ")"#- switchBlock )
| ~("Return" "return"#-#E expr? ";"#-)
| ~("Break" "break"#-#E ";"#-)
| ~("Continue" "continue"#-#E ";"#-)
| ("{"#E#- statement*#e "}"#E#- )#p
| ~("Expr" expr) ";"#-
)#e#p;
/* END RULES */
`
var km = make(map[string]string)
var synfile syntaxfile.SyntaxFile = nil
func init(){
synfile = syntaxfile.Parse(strings.NewReader(rules))
synfile.ScanForKeyWords(km)
}
func Parse(src io.Reader) (*ast.AST,*syntaxfile.ErrorRecorder) {
a := new(ast.AST)
e := new(syntaxfile.ErrorRecorder)
s := new(parsing.StdScanner)
s.Init(km,src)
ft := s.FirstToken()
synfile["srcfile"].Parse(synfile,ft,a,e)
return a,e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment