commit
b8456329aa
20 changed files with 2758 additions and 4719 deletions
18
Godeps/Godeps.json
generated
18
Godeps/Godeps.json
generated
|
@ -1,22 +1,18 @@
|
|||
{
|
||||
"ImportPath": "github.com/leonelquinteros/gotext",
|
||||
"GoVersion": "go1.6",
|
||||
"GodepVersion": "v74",
|
||||
"Packages": [
|
||||
"./..."
|
||||
],
|
||||
"GoVersion": "devel +be9d7f6d87 Fri Jun 16 00:31:25 2017 +0000",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/mattn/anko/ast",
|
||||
"Rev": "a8c68fa2983e7dd5d3472992b1fbe2f7c44261f0"
|
||||
"ImportPath": "github.com/mattn/kinako/ast",
|
||||
"Rev": "fbc18625ec69b28ae03b4eea4c349f215840ee09"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mattn/anko/parser",
|
||||
"Rev": "a8c68fa2983e7dd5d3472992b1fbe2f7c44261f0"
|
||||
"ImportPath": "github.com/mattn/kinako/parser",
|
||||
"Rev": "fbc18625ec69b28ae03b4eea4c349f215840ee09"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mattn/anko/vm",
|
||||
"Rev": "a8c68fa2983e7dd5d3472992b1fbe2f7c44261f0"
|
||||
"ImportPath": "github.com/mattn/kinako/vm",
|
||||
"Rev": "fbc18625ec69b28ae03b4eea4c349f215840ee09"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -265,7 +265,7 @@ PO format supports defining one or more plural forms for the same translation.
|
|||
Relying on the PO file headers, a Plural-Forms formula can be set on the translation file
|
||||
as defined in (https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html)
|
||||
|
||||
Plural formulas are parsed and evaluated using [Anko](https://github.com/mattn/anko)
|
||||
Plural formulas are parsed and evaluated using [Kinako](https://github.com/mattn/kinako)
|
||||
|
||||
```go
|
||||
import "github.com/leonelquinteros/gotext"
|
||||
|
|
3
po.go
3
po.go
|
@ -3,13 +3,14 @@ package gotext
|
|||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/mattn/anko/vm"
|
||||
"io/ioutil"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/mattn/kinako/vm"
|
||||
)
|
||||
|
||||
type translation struct {
|
||||
|
|
2
vendor/github.com/mattn/anko/ast/doc.go
generated
vendored
2
vendor/github.com/mattn/anko/ast/doc.go
generated
vendored
|
@ -1,2 +0,0 @@
|
|||
// Package ast implements abstruct-syntax-tree for anko.
|
||||
package ast
|
201
vendor/github.com/mattn/anko/ast/expr.go
generated
vendored
201
vendor/github.com/mattn/anko/ast/expr.go
generated
vendored
|
@ -1,201 +0,0 @@
|
|||
package ast
|
||||
|
||||
// Expr provides all of interfaces for expression.
|
||||
type Expr interface {
|
||||
Pos
|
||||
expr()
|
||||
}
|
||||
|
||||
// ExprImpl provide commonly implementations for Expr.
|
||||
type ExprImpl struct {
|
||||
PosImpl // ExprImpl provide Pos() function.
|
||||
}
|
||||
|
||||
// expr provide restraint interface.
|
||||
func (x *ExprImpl) expr() {}
|
||||
|
||||
// NumberExpr provide Number expression.
|
||||
type NumberExpr struct {
|
||||
ExprImpl
|
||||
Lit string
|
||||
}
|
||||
|
||||
// StringExpr provide String expression.
|
||||
type StringExpr struct {
|
||||
ExprImpl
|
||||
Lit string
|
||||
}
|
||||
|
||||
// ArrayExpr provide Array expression.
|
||||
type ArrayExpr struct {
|
||||
ExprImpl
|
||||
Exprs []Expr
|
||||
}
|
||||
|
||||
// PairExpr provide one of Map key/value pair.
|
||||
type PairExpr struct {
|
||||
ExprImpl
|
||||
Key string
|
||||
Value Expr
|
||||
}
|
||||
|
||||
// MapExpr provide Map expression.
|
||||
type MapExpr struct {
|
||||
ExprImpl
|
||||
MapExpr map[string]Expr
|
||||
}
|
||||
|
||||
// IdentExpr provide identity expression.
|
||||
type IdentExpr struct {
|
||||
ExprImpl
|
||||
Lit string
|
||||
}
|
||||
|
||||
// UnaryExpr provide unary minus expression. ex: -1, ^1, ~1.
|
||||
type UnaryExpr struct {
|
||||
ExprImpl
|
||||
Operator string
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// AddrExpr provide referencing address expression.
|
||||
type AddrExpr struct {
|
||||
ExprImpl
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// DerefExpr provide dereferencing address expression.
|
||||
type DerefExpr struct {
|
||||
ExprImpl
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// ParenExpr provide parent block expression.
|
||||
type ParenExpr struct {
|
||||
ExprImpl
|
||||
SubExpr Expr
|
||||
}
|
||||
|
||||
// BinOpExpr provide binary operator expression.
|
||||
type BinOpExpr struct {
|
||||
ExprImpl
|
||||
Lhs Expr
|
||||
Operator string
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
type TernaryOpExpr struct {
|
||||
ExprImpl
|
||||
Expr Expr
|
||||
Lhs Expr
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
// CallExpr provide calling expression.
|
||||
type CallExpr struct {
|
||||
ExprImpl
|
||||
Func interface{}
|
||||
Name string
|
||||
SubExprs []Expr
|
||||
VarArg bool
|
||||
Go bool
|
||||
}
|
||||
|
||||
// AnonCallExpr provide anonymous calling expression. ex: func(){}().
|
||||
type AnonCallExpr struct {
|
||||
ExprImpl
|
||||
Expr Expr
|
||||
SubExprs []Expr
|
||||
VarArg bool
|
||||
Go bool
|
||||
}
|
||||
|
||||
// MemberExpr provide expression to refer menber.
|
||||
type MemberExpr struct {
|
||||
ExprImpl
|
||||
Expr Expr
|
||||
Name string
|
||||
}
|
||||
|
||||
// ItemExpr provide expression to refer Map/Array item.
|
||||
type ItemExpr struct {
|
||||
ExprImpl
|
||||
Value Expr
|
||||
Index Expr
|
||||
}
|
||||
|
||||
// SliceExpr provide expression to refer slice of Array.
|
||||
type SliceExpr struct {
|
||||
ExprImpl
|
||||
Value Expr
|
||||
Begin Expr
|
||||
End Expr
|
||||
}
|
||||
|
||||
// FuncExpr provide function expression.
|
||||
type FuncExpr struct {
|
||||
ExprImpl
|
||||
Name string
|
||||
Stmts []Stmt
|
||||
Args []string
|
||||
VarArg bool
|
||||
}
|
||||
|
||||
// LetExpr provide expression to let variable.
|
||||
type LetExpr struct {
|
||||
ExprImpl
|
||||
Lhs Expr
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
// LetsExpr provide multiple expression of let.
|
||||
type LetsExpr struct {
|
||||
ExprImpl
|
||||
Lhss []Expr
|
||||
Operator string
|
||||
Rhss []Expr
|
||||
}
|
||||
|
||||
// AssocExpr provide expression to assoc operation.
|
||||
type AssocExpr struct {
|
||||
ExprImpl
|
||||
Lhs Expr
|
||||
Operator string
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
// NewExpr provide expression to make new instance.
|
||||
type NewExpr struct {
|
||||
ExprImpl
|
||||
Name string
|
||||
SubExprs []Expr
|
||||
}
|
||||
|
||||
// ConstExpr provide expression for constant variable.
|
||||
type ConstExpr struct {
|
||||
ExprImpl
|
||||
Value string
|
||||
}
|
||||
|
||||
type ChanExpr struct {
|
||||
ExprImpl
|
||||
Lhs Expr
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
type Type struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type MakeChanExpr struct {
|
||||
ExprImpl
|
||||
Type string
|
||||
SizeExpr Expr
|
||||
}
|
||||
|
||||
type MakeArrayExpr struct {
|
||||
ExprImpl
|
||||
Type string
|
||||
LenExpr Expr
|
||||
CapExpr Expr
|
||||
}
|
28
vendor/github.com/mattn/anko/ast/pos.go
generated
vendored
28
vendor/github.com/mattn/anko/ast/pos.go
generated
vendored
|
@ -1,28 +0,0 @@
|
|||
package ast
|
||||
|
||||
// Position provides interface to store code locations.
|
||||
type Position struct {
|
||||
Line int
|
||||
Column int
|
||||
}
|
||||
|
||||
// Pos interface provies two functions to get/set the position for expression or statement.
|
||||
type Pos interface {
|
||||
Position() Position
|
||||
SetPosition(Position)
|
||||
}
|
||||
|
||||
// PosImpl provies commonly implementations for Pos.
|
||||
type PosImpl struct {
|
||||
pos Position
|
||||
}
|
||||
|
||||
// Position return the position of the expression or statement.
|
||||
func (x *PosImpl) Position() Position {
|
||||
return x.pos
|
||||
}
|
||||
|
||||
// SetPosition is a function to specify position of the expression or statement.
|
||||
func (x *PosImpl) SetPosition(pos Position) {
|
||||
x.pos = pos
|
||||
}
|
127
vendor/github.com/mattn/anko/ast/stmt.go
generated
vendored
127
vendor/github.com/mattn/anko/ast/stmt.go
generated
vendored
|
@ -1,127 +0,0 @@
|
|||
package ast
|
||||
|
||||
// Stmt provides all of interfaces for statement.
|
||||
type Stmt interface {
|
||||
Pos
|
||||
stmt()
|
||||
}
|
||||
|
||||
// StmtImpl provide commonly implementations for Stmt..
|
||||
type StmtImpl struct {
|
||||
PosImpl // StmtImpl provide Pos() function.
|
||||
}
|
||||
|
||||
// stmt provide restraint interface.
|
||||
func (x *StmtImpl) stmt() {}
|
||||
|
||||
// ExprStmt provide expression statement.
|
||||
type ExprStmt struct {
|
||||
StmtImpl
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// IfStmt provide "if/else" statement.
|
||||
type IfStmt struct {
|
||||
StmtImpl
|
||||
If Expr
|
||||
Then []Stmt
|
||||
ElseIf []Stmt // This is array of IfStmt
|
||||
Else []Stmt
|
||||
}
|
||||
|
||||
// TryStmt provide "try/catch/finally" statement.
|
||||
type TryStmt struct {
|
||||
StmtImpl
|
||||
Try []Stmt
|
||||
Var string
|
||||
Catch []Stmt
|
||||
Finally []Stmt
|
||||
}
|
||||
|
||||
// ForStmt provide "for in" expression statement.
|
||||
type ForStmt struct {
|
||||
StmtImpl
|
||||
Var string
|
||||
Value Expr
|
||||
Stmts []Stmt
|
||||
}
|
||||
|
||||
// CForStmt provide C-style "for (;;)" expression statement.
|
||||
type CForStmt struct {
|
||||
StmtImpl
|
||||
Expr1 Expr
|
||||
Expr2 Expr
|
||||
Expr3 Expr
|
||||
Stmts []Stmt
|
||||
}
|
||||
|
||||
// LoopStmt provide "for expr" expression statement.
|
||||
type LoopStmt struct {
|
||||
StmtImpl
|
||||
Expr Expr
|
||||
Stmts []Stmt
|
||||
}
|
||||
|
||||
// BreakStmt provide "break" expression statement.
|
||||
type BreakStmt struct {
|
||||
StmtImpl
|
||||
}
|
||||
|
||||
// ContinueStmt provide "continue" expression statement.
|
||||
type ContinueStmt struct {
|
||||
StmtImpl
|
||||
}
|
||||
|
||||
// ForStmt provide "return" expression statement.
|
||||
type ReturnStmt struct {
|
||||
StmtImpl
|
||||
Exprs []Expr
|
||||
}
|
||||
|
||||
// ThrowStmt provide "throw" expression statement.
|
||||
type ThrowStmt struct {
|
||||
StmtImpl
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// ModuleStmt provide "module" expression statement.
|
||||
type ModuleStmt struct {
|
||||
StmtImpl
|
||||
Name string
|
||||
Stmts []Stmt
|
||||
}
|
||||
|
||||
// VarStmt provide statement to let variables in current scope.
|
||||
type VarStmt struct {
|
||||
StmtImpl
|
||||
Names []string
|
||||
Exprs []Expr
|
||||
}
|
||||
|
||||
// SwitchStmt provide switch statement.
|
||||
type SwitchStmt struct {
|
||||
StmtImpl
|
||||
Expr Expr
|
||||
Cases []Stmt
|
||||
}
|
||||
|
||||
// CaseStmt provide switch/case statement.
|
||||
type CaseStmt struct {
|
||||
StmtImpl
|
||||
Expr Expr
|
||||
Stmts []Stmt
|
||||
}
|
||||
|
||||
// DefaultStmt provide switch/default statement.
|
||||
type DefaultStmt struct {
|
||||
StmtImpl
|
||||
Stmts []Stmt
|
||||
}
|
||||
|
||||
// LetsStmt provide multiple statement of let.
|
||||
type LetsStmt struct {
|
||||
StmtImpl
|
||||
Lhss []Expr
|
||||
Operator string
|
||||
Rhss []Expr
|
||||
}
|
7
vendor/github.com/mattn/anko/ast/token.go
generated
vendored
7
vendor/github.com/mattn/anko/ast/token.go
generated
vendored
|
@ -1,7 +0,0 @@
|
|||
package ast
|
||||
|
||||
type Token struct {
|
||||
PosImpl // StmtImpl provide Pos() function.
|
||||
Tok int
|
||||
Lit string
|
||||
}
|
1997
vendor/github.com/mattn/anko/parser/parser.go
generated
vendored
1997
vendor/github.com/mattn/anko/parser/parser.go
generated
vendored
File diff suppressed because it is too large
Load diff
705
vendor/github.com/mattn/anko/parser/parser.go.y
generated
vendored
705
vendor/github.com/mattn/anko/parser/parser.go.y
generated
vendored
|
@ -1,705 +0,0 @@
|
|||
%{
|
||||
package parser
|
||||
|
||||
import (
|
||||
"github.com/mattn/anko/ast"
|
||||
)
|
||||
|
||||
%}
|
||||
|
||||
%type<compstmt> compstmt
|
||||
%type<stmts> stmts
|
||||
%type<stmt> stmt
|
||||
%type<stmt_if> stmt_if
|
||||
%type<stmt_default> stmt_default
|
||||
%type<stmt_case> stmt_case
|
||||
%type<stmt_cases> stmt_cases
|
||||
%type<typ> typ
|
||||
%type<expr> expr
|
||||
%type<exprs> exprs
|
||||
%type<expr_many> expr_many
|
||||
%type<expr_lets> expr_lets
|
||||
%type<expr_pair> expr_pair
|
||||
%type<expr_pairs> expr_pairs
|
||||
%type<expr_idents> expr_idents
|
||||
|
||||
%union{
|
||||
compstmt []ast.Stmt
|
||||
stmt_if ast.Stmt
|
||||
stmt_default ast.Stmt
|
||||
stmt_case ast.Stmt
|
||||
stmt_cases []ast.Stmt
|
||||
stmts []ast.Stmt
|
||||
stmt ast.Stmt
|
||||
typ ast.Type
|
||||
expr ast.Expr
|
||||
exprs []ast.Expr
|
||||
expr_many []ast.Expr
|
||||
expr_lets ast.Expr
|
||||
expr_pair ast.Expr
|
||||
expr_pairs []ast.Expr
|
||||
expr_idents []string
|
||||
tok ast.Token
|
||||
term ast.Token
|
||||
terms ast.Token
|
||||
opt_terms ast.Token
|
||||
}
|
||||
|
||||
%token<tok> IDENT NUMBER STRING ARRAY VARARG FUNC RETURN VAR THROW IF ELSE FOR IN EQEQ NEQ GE LE OROR ANDAND NEW TRUE FALSE NIL MODULE TRY CATCH FINALLY PLUSEQ MINUSEQ MULEQ DIVEQ ANDEQ OREQ BREAK CONTINUE PLUSPLUS MINUSMINUS POW SHIFTLEFT SHIFTRIGHT SWITCH CASE DEFAULT GO CHAN MAKE OPCHAN ARRAYLIT
|
||||
|
||||
%right '='
|
||||
%right '?' ':'
|
||||
%left OROR
|
||||
%left ANDAND
|
||||
%left IDENT
|
||||
%nonassoc EQEQ NEQ ','
|
||||
%left '>' GE '<' LE SHIFTLEFT SHIFTRIGHT
|
||||
|
||||
%left '+' '-' PLUSPLUS MINUSMINUS
|
||||
%left '*' '/' '%'
|
||||
%right UNARY
|
||||
|
||||
%%
|
||||
|
||||
compstmt : opt_terms
|
||||
{
|
||||
$$ = nil
|
||||
}
|
||||
| stmts opt_terms
|
||||
{
|
||||
$$ = $1
|
||||
}
|
||||
|
||||
stmts :
|
||||
{
|
||||
$$ = nil
|
||||
if l, ok := yylex.(*Lexer); ok {
|
||||
l.stmts = $$
|
||||
}
|
||||
}
|
||||
| opt_terms stmt
|
||||
{
|
||||
$$ = []ast.Stmt{$2}
|
||||
if l, ok := yylex.(*Lexer); ok {
|
||||
l.stmts = $$
|
||||
}
|
||||
}
|
||||
| stmts terms stmt
|
||||
{
|
||||
if $3 != nil {
|
||||
$$ = append($1, $3)
|
||||
if l, ok := yylex.(*Lexer); ok {
|
||||
l.stmts = $$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stmt :
|
||||
VAR expr_idents '=' expr_many
|
||||
{
|
||||
$$ = &ast.VarStmt{Names: $2, Exprs: $4}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '=' expr
|
||||
{
|
||||
$$ = &ast.LetsStmt{Lhss: []ast.Expr{$1}, Operator: "=", Rhss: []ast.Expr{$3}}
|
||||
}
|
||||
| expr_many '=' expr_many
|
||||
{
|
||||
$$ = &ast.LetsStmt{Lhss: $1, Operator: "=", Rhss: $3}
|
||||
}
|
||||
| BREAK
|
||||
{
|
||||
$$ = &ast.BreakStmt{}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| CONTINUE
|
||||
{
|
||||
$$ = &ast.ContinueStmt{}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| RETURN exprs
|
||||
{
|
||||
$$ = &ast.ReturnStmt{Exprs: $2}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| THROW expr
|
||||
{
|
||||
$$ = &ast.ThrowStmt{Expr: $2}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| MODULE IDENT '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.ModuleStmt{Name: $2.Lit, Stmts: $4}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| stmt_if
|
||||
{
|
||||
$$ = $1
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FOR '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.LoopStmt{Stmts: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FOR IDENT IN expr '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.ForStmt{Var: $2.Lit, Value: $4, Stmts: $6}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FOR expr_lets ';' expr ';' expr '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.CForStmt{Expr1: $2, Expr2: $4, Expr3: $6, Stmts: $8}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FOR expr '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.LoopStmt{Expr: $2, Stmts: $4}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| TRY '{' compstmt '}' CATCH IDENT '{' compstmt '}' FINALLY '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.TryStmt{Try: $3, Var: $6.Lit, Catch: $8, Finally: $12}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| TRY '{' compstmt '}' CATCH '{' compstmt '}' FINALLY '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.TryStmt{Try: $3, Catch: $7, Finally: $11}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| TRY '{' compstmt '}' CATCH IDENT '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.TryStmt{Try: $3, Var: $6.Lit, Catch: $8}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| TRY '{' compstmt '}' CATCH '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.TryStmt{Try: $3, Catch: $7}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| SWITCH expr '{' stmt_cases '}'
|
||||
{
|
||||
$$ = &ast.SwitchStmt{Expr: $2, Cases: $4}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr
|
||||
{
|
||||
$$ = &ast.ExprStmt{Expr: $1}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
|
||||
|
||||
stmt_if :
|
||||
stmt_if ELSE IF expr '{' compstmt '}'
|
||||
{
|
||||
$1.(*ast.IfStmt).ElseIf = append($1.(*ast.IfStmt).ElseIf, &ast.IfStmt{If: $4, Then: $6})
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| stmt_if ELSE '{' compstmt '}'
|
||||
{
|
||||
if $$.(*ast.IfStmt).Else != nil {
|
||||
yylex.Error("multiple else statement")
|
||||
} else {
|
||||
$$.(*ast.IfStmt).Else = append($$.(*ast.IfStmt).Else, $4...)
|
||||
}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| IF expr '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.IfStmt{If: $2, Then: $4, Else: nil}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
|
||||
stmt_cases :
|
||||
{
|
||||
$$ = []ast.Stmt{}
|
||||
}
|
||||
| opt_terms stmt_case
|
||||
{
|
||||
$$ = []ast.Stmt{$2}
|
||||
}
|
||||
| opt_terms stmt_default
|
||||
{
|
||||
$$ = []ast.Stmt{$2}
|
||||
}
|
||||
| stmt_cases stmt_case
|
||||
{
|
||||
$$ = append($1, $2)
|
||||
}
|
||||
| stmt_cases stmt_default
|
||||
{
|
||||
for _, stmt := range $1 {
|
||||
if _, ok := stmt.(*ast.DefaultStmt); ok {
|
||||
yylex.Error("multiple default statement")
|
||||
}
|
||||
}
|
||||
$$ = append($1, $2)
|
||||
}
|
||||
|
||||
stmt_case :
|
||||
CASE expr ':' opt_terms compstmt
|
||||
{
|
||||
$$ = &ast.CaseStmt{Expr: $2, Stmts: $5}
|
||||
}
|
||||
|
||||
stmt_default :
|
||||
DEFAULT ':' opt_terms compstmt
|
||||
{
|
||||
$$ = &ast.DefaultStmt{Stmts: $4}
|
||||
}
|
||||
|
||||
expr_pair :
|
||||
STRING ':' expr
|
||||
{
|
||||
$$ = &ast.PairExpr{Key: $1.Lit, Value: $3}
|
||||
}
|
||||
|
||||
expr_pairs :
|
||||
{
|
||||
$$ = []ast.Expr{}
|
||||
}
|
||||
| expr_pair
|
||||
{
|
||||
$$ = []ast.Expr{$1}
|
||||
}
|
||||
| expr_pairs ',' opt_terms expr_pair
|
||||
{
|
||||
$$ = append($1, $4)
|
||||
}
|
||||
|
||||
expr_idents :
|
||||
{
|
||||
$$ = []string{}
|
||||
}
|
||||
| IDENT
|
||||
{
|
||||
$$ = []string{$1.Lit}
|
||||
}
|
||||
| expr_idents ',' opt_terms IDENT
|
||||
{
|
||||
$$ = append($1, $4.Lit)
|
||||
}
|
||||
|
||||
expr_lets : expr_many '=' expr_many
|
||||
{
|
||||
$$ = &ast.LetsExpr{Lhss: $1, Operator: "=", Rhss: $3}
|
||||
}
|
||||
|
||||
expr_many :
|
||||
expr
|
||||
{
|
||||
$$ = []ast.Expr{$1}
|
||||
}
|
||||
| exprs ',' opt_terms expr
|
||||
{
|
||||
$$ = append($1, $4)
|
||||
}
|
||||
| exprs ',' opt_terms IDENT
|
||||
{
|
||||
$$ = append($1, &ast.IdentExpr{Lit: $4.Lit})
|
||||
}
|
||||
|
||||
typ : IDENT
|
||||
{
|
||||
$$ = ast.Type{Name: $1.Lit}
|
||||
}
|
||||
| typ '.' IDENT
|
||||
{
|
||||
$$ = ast.Type{Name: $1.Name + "." + $3.Lit}
|
||||
}
|
||||
|
||||
exprs :
|
||||
{
|
||||
$$ = nil
|
||||
}
|
||||
| expr
|
||||
{
|
||||
$$ = []ast.Expr{$1}
|
||||
}
|
||||
| exprs ',' opt_terms expr
|
||||
{
|
||||
$$ = append($1, $4)
|
||||
}
|
||||
| exprs ',' opt_terms IDENT
|
||||
{
|
||||
$$ = append($1, &ast.IdentExpr{Lit: $4.Lit})
|
||||
}
|
||||
|
||||
expr :
|
||||
IDENT
|
||||
{
|
||||
$$ = &ast.IdentExpr{Lit: $1.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| NUMBER
|
||||
{
|
||||
$$ = &ast.NumberExpr{Lit: $1.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| '-' expr %prec UNARY
|
||||
{
|
||||
$$ = &ast.UnaryExpr{Operator: "-", Expr: $2}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| '!' expr %prec UNARY
|
||||
{
|
||||
$$ = &ast.UnaryExpr{Operator: "!", Expr: $2}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| '^' expr %prec UNARY
|
||||
{
|
||||
$$ = &ast.UnaryExpr{Operator: "^", Expr: $2}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| '&' IDENT %prec UNARY
|
||||
{
|
||||
$$ = &ast.AddrExpr{Expr: &ast.IdentExpr{Lit: $2.Lit}}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| '&' expr '.' IDENT %prec UNARY
|
||||
{
|
||||
$$ = &ast.AddrExpr{Expr: &ast.MemberExpr{Expr: $2, Name: $4.Lit}}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| '*' IDENT %prec UNARY
|
||||
{
|
||||
$$ = &ast.DerefExpr{Expr: &ast.IdentExpr{Lit: $2.Lit}}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| '*' expr '.' IDENT %prec UNARY
|
||||
{
|
||||
$$ = &ast.DerefExpr{Expr: &ast.MemberExpr{Expr: $2, Name: $4.Lit}}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| STRING
|
||||
{
|
||||
$$ = &ast.StringExpr{Lit: $1.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| TRUE
|
||||
{
|
||||
$$ = &ast.ConstExpr{Value: $1.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FALSE
|
||||
{
|
||||
$$ = &ast.ConstExpr{Value: $1.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| NIL
|
||||
{
|
||||
$$ = &ast.ConstExpr{Value: $1.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '?' expr ':' expr
|
||||
{
|
||||
$$ = &ast.TernaryOpExpr{Expr: $1, Lhs: $3, Rhs: $5}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '.' IDENT
|
||||
{
|
||||
$$ = &ast.MemberExpr{Expr: $1, Name: $3.Lit}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FUNC '(' expr_idents ')' '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.FuncExpr{Args: $3, Stmts: $6}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FUNC '(' IDENT VARARG ')' '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.FuncExpr{Args: []string{$3.Lit}, Stmts: $7, VarArg: true}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FUNC IDENT '(' expr_idents ')' '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.FuncExpr{Name: $2.Lit, Args: $4, Stmts: $7}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| FUNC IDENT '(' IDENT VARARG ')' '{' compstmt '}'
|
||||
{
|
||||
$$ = &ast.FuncExpr{Name: $2.Lit, Args: []string{$4.Lit}, Stmts: $8, VarArg: true}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| '[' opt_terms exprs opt_terms ']'
|
||||
{
|
||||
$$ = &ast.ArrayExpr{Exprs: $3}
|
||||
if l, ok := yylex.(*Lexer); ok { $$.SetPosition(l.pos) }
|
||||
}
|
||||
| '[' opt_terms exprs ',' opt_terms ']'
|
||||
{
|
||||
$$ = &ast.ArrayExpr{Exprs: $3}
|
||||
if l, ok := yylex.(*Lexer); ok { $$.SetPosition(l.pos) }
|
||||
}
|
||||
| '{' opt_terms expr_pairs opt_terms '}'
|
||||
{
|
||||
mapExpr := make(map[string]ast.Expr)
|
||||
for _, v := range $3 {
|
||||
mapExpr[v.(*ast.PairExpr).Key] = v.(*ast.PairExpr).Value
|
||||
}
|
||||
$$ = &ast.MapExpr{MapExpr: mapExpr}
|
||||
if l, ok := yylex.(*Lexer); ok { $$.SetPosition(l.pos) }
|
||||
}
|
||||
| '{' opt_terms expr_pairs ',' opt_terms '}'
|
||||
{
|
||||
mapExpr := make(map[string]ast.Expr)
|
||||
for _, v := range $3 {
|
||||
mapExpr[v.(*ast.PairExpr).Key] = v.(*ast.PairExpr).Value
|
||||
}
|
||||
$$ = &ast.MapExpr{MapExpr: mapExpr}
|
||||
if l, ok := yylex.(*Lexer); ok { $$.SetPosition(l.pos) }
|
||||
}
|
||||
| '(' expr ')'
|
||||
{
|
||||
$$ = &ast.ParenExpr{SubExpr: $2}
|
||||
if l, ok := yylex.(*Lexer); ok { $$.SetPosition(l.pos) }
|
||||
}
|
||||
| NEW IDENT '(' exprs ')'
|
||||
{
|
||||
$$ = &ast.NewExpr{Name: $2.Lit, SubExprs: $4}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '+' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "+", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '-' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "-", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '*' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "*", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '/' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "/", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '%' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "%", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr POW expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "**", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr SHIFTLEFT expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "<<", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr SHIFTRIGHT expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: ">>", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr EQEQ expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "==", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr NEQ expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "!=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '>' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: ">", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr GE expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: ">=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '<' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "<", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr LE expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "<=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr PLUSEQ expr
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "+=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr MINUSEQ expr
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "-=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr MULEQ expr
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "*=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr DIVEQ expr
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "/=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr ANDEQ expr
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "&=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr OREQ expr
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "|=", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr PLUSPLUS
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "++"}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr MINUSMINUS
|
||||
{
|
||||
$$ = &ast.AssocExpr{Lhs: $1, Operator: "--"}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '|' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "|", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr OROR expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "||", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '&' expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "&", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr ANDAND expr
|
||||
{
|
||||
$$ = &ast.BinOpExpr{Lhs: $1, Operator: "&&", Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| IDENT '(' exprs VARARG ')'
|
||||
{
|
||||
$$ = &ast.CallExpr{Name: $1.Lit, SubExprs: $3, VarArg: true}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| IDENT '(' exprs ')'
|
||||
{
|
||||
$$ = &ast.CallExpr{Name: $1.Lit, SubExprs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| GO IDENT '(' exprs VARARG ')'
|
||||
{
|
||||
$$ = &ast.CallExpr{Name: $2.Lit, SubExprs: $4, VarArg: true, Go: true}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| GO IDENT '(' exprs ')'
|
||||
{
|
||||
$$ = &ast.CallExpr{Name: $2.Lit, SubExprs: $4, Go: true}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| expr '(' exprs VARARG ')'
|
||||
{
|
||||
$$ = &ast.AnonCallExpr{Expr: $1, SubExprs: $3, VarArg: true}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '(' exprs ')'
|
||||
{
|
||||
$$ = &ast.AnonCallExpr{Expr: $1, SubExprs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| GO expr '(' exprs VARARG ')'
|
||||
{
|
||||
$$ = &ast.AnonCallExpr{Expr: $2, SubExprs: $4, VarArg: true, Go: true}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
| GO expr '(' exprs ')'
|
||||
{
|
||||
$$ = &ast.AnonCallExpr{Expr: $2, SubExprs: $4, Go: true}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| IDENT '[' expr ']'
|
||||
{
|
||||
$$ = &ast.ItemExpr{Value: &ast.IdentExpr{Lit: $1.Lit}, Index: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '[' expr ']'
|
||||
{
|
||||
$$ = &ast.ItemExpr{Value: $1, Index: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| IDENT '[' expr ':' expr ']'
|
||||
{
|
||||
$$ = &ast.SliceExpr{Value: &ast.IdentExpr{Lit: $1.Lit}, Begin: $3, End: $5}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr '[' expr ':' expr ']'
|
||||
{
|
||||
$$ = &ast.SliceExpr{Value: $1, Begin: $3, End: $5}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| MAKE '(' CHAN typ ')'
|
||||
{
|
||||
$$ = &ast.MakeChanExpr{Type: $4.Name, SizeExpr: nil}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| MAKE '(' CHAN typ ',' expr ')'
|
||||
{
|
||||
$$ = &ast.MakeChanExpr{Type: $4.Name, SizeExpr: $6}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| MAKE '(' ARRAYLIT typ ',' expr ')'
|
||||
{
|
||||
$$ = &ast.MakeArrayExpr{Type: $4.Name, LenExpr: $6}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| MAKE '(' ARRAYLIT typ ',' expr ',' expr ')'
|
||||
{
|
||||
$$ = &ast.MakeArrayExpr{Type: $4.Name, LenExpr: $6, CapExpr: $8}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| expr OPCHAN expr
|
||||
{
|
||||
$$ = &ast.ChanExpr{Lhs: $1, Rhs: $3}
|
||||
$$.SetPosition($1.Position())
|
||||
}
|
||||
| OPCHAN expr
|
||||
{
|
||||
$$ = &ast.ChanExpr{Rhs: $2}
|
||||
$$.SetPosition($2.Position())
|
||||
}
|
||||
|
||||
opt_terms : /* none */
|
||||
| terms
|
||||
;
|
||||
|
||||
|
||||
terms : term
|
||||
{
|
||||
}
|
||||
| terms term
|
||||
{
|
||||
}
|
||||
;
|
||||
|
||||
term : ';'
|
||||
{
|
||||
}
|
||||
| '\n'
|
||||
{
|
||||
}
|
||||
;
|
||||
|
||||
%%
|
2
vendor/github.com/mattn/anko/vm/doc.go
generated
vendored
2
vendor/github.com/mattn/anko/vm/doc.go
generated
vendored
|
@ -1,2 +0,0 @@
|
|||
// Package vm implements virtual-machine for anko.
|
||||
package vm
|
1504
vendor/github.com/mattn/anko/vm/vm.go
generated
vendored
1504
vendor/github.com/mattn/anko/vm/vm.go
generated
vendored
File diff suppressed because it is too large
Load diff
104
vendor/github.com/mattn/kinako/ast/expr.go
generated
vendored
Normal file
104
vendor/github.com/mattn/kinako/ast/expr.go
generated
vendored
Normal file
|
@ -0,0 +1,104 @@
|
|||
package ast
|
||||
|
||||
type Token struct {
|
||||
Tok int
|
||||
Lit string
|
||||
}
|
||||
|
||||
// Position provides interface to store code locations.
|
||||
type Position struct {
|
||||
Line int
|
||||
Column int
|
||||
}
|
||||
|
||||
// Expr provides all of interfaces for expression.
|
||||
type Expr interface {
|
||||
expr()
|
||||
}
|
||||
|
||||
// ExprImpl provide commonly implementations for Expr.
|
||||
type ExprImpl struct {
|
||||
}
|
||||
|
||||
// expr provide restraint interface.
|
||||
func (x *ExprImpl) expr() {}
|
||||
|
||||
// NumberExpr provide Number expression.
|
||||
type NumberExpr struct {
|
||||
ExprImpl
|
||||
Lit string
|
||||
}
|
||||
|
||||
// UnaryExpr provide unary minus expression. ex: -1, ^1, ~1.
|
||||
type UnaryExpr struct {
|
||||
ExprImpl
|
||||
Operator string
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// IdentExpr provide identity expression.
|
||||
type IdentExpr struct {
|
||||
ExprImpl
|
||||
Lit string
|
||||
}
|
||||
|
||||
// Stmt provides all of interfaces for statement.
|
||||
type Stmt interface {
|
||||
stmt()
|
||||
}
|
||||
|
||||
// StmtImpl provide commonly implementations for Stmt..
|
||||
type StmtImpl struct {
|
||||
}
|
||||
|
||||
// stmt provide restraint interface.
|
||||
func (x *StmtImpl) stmt() {}
|
||||
|
||||
// LetsStmt provide multiple statement of let.
|
||||
type LetsStmt struct {
|
||||
StmtImpl
|
||||
Lhss []Expr
|
||||
Operator string
|
||||
Rhss []Expr
|
||||
}
|
||||
|
||||
// StringExpr provide String expression.
|
||||
type StringExpr struct {
|
||||
ExprImpl
|
||||
Lit string
|
||||
}
|
||||
|
||||
type TernaryOpExpr struct {
|
||||
ExprImpl
|
||||
Expr Expr
|
||||
Lhs Expr
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
// ParenExpr provide parent block expression.
|
||||
type ParenExpr struct {
|
||||
ExprImpl
|
||||
SubExpr Expr
|
||||
}
|
||||
|
||||
// BinOpExpr provide binary operator expression.
|
||||
type BinOpExpr struct {
|
||||
ExprImpl
|
||||
Lhs Expr
|
||||
Operator string
|
||||
Rhs Expr
|
||||
}
|
||||
|
||||
// ExprStmt provide expression statement.
|
||||
type ExprStmt struct {
|
||||
StmtImpl
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
// LetStmt provide statement of let.
|
||||
type LetStmt struct {
|
||||
StmtImpl
|
||||
Lhs Expr
|
||||
Operator string
|
||||
Rhs Expr
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
all : parser.go
|
||||
|
||||
parser.go : parser.go.y
|
||||
go tool yacc -o $@ parser.go.y
|
||||
goyacc -o $@ parser.go.y
|
|
@ -1,11 +1,11 @@
|
|||
// Package parser implements parser for anko.
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"unicode"
|
||||
|
||||
"github.com/mattn/anko/ast"
|
||||
"github.com/mattn/kinako/ast"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -17,7 +17,6 @@ const (
|
|||
// It can be Error inteface with type cast which can call Pos().
|
||||
type Error struct {
|
||||
Message string
|
||||
Pos ast.Position
|
||||
Filename string
|
||||
Fatal bool
|
||||
}
|
||||
|
@ -35,34 +34,6 @@ type Scanner struct {
|
|||
line int
|
||||
}
|
||||
|
||||
// opName is correction of operation names.
|
||||
var opName = map[string]int{
|
||||
"func": FUNC,
|
||||
"return": RETURN,
|
||||
"var": VAR,
|
||||
"throw": THROW,
|
||||
"if": IF,
|
||||
"for": FOR,
|
||||
"break": BREAK,
|
||||
"continue": CONTINUE,
|
||||
"in": IN,
|
||||
"else": ELSE,
|
||||
"new": NEW,
|
||||
"true": TRUE,
|
||||
"false": FALSE,
|
||||
"nil": NIL,
|
||||
"module": MODULE,
|
||||
"try": TRY,
|
||||
"catch": CATCH,
|
||||
"finally": FINALLY,
|
||||
"switch": SWITCH,
|
||||
"case": CASE,
|
||||
"default": DEFAULT,
|
||||
"go": GO,
|
||||
"chan": CHAN,
|
||||
"make": MAKE,
|
||||
}
|
||||
|
||||
// Init resets code to scan.
|
||||
func (s *Scanner) Init(src string) {
|
||||
s.src = []rune(src)
|
||||
|
@ -75,15 +46,11 @@ retry:
|
|||
pos = s.pos()
|
||||
switch ch := s.peek(); {
|
||||
case isLetter(ch):
|
||||
tok = IDENT
|
||||
lit, err = s.scanIdentifier()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if name, ok := opName[lit]; ok {
|
||||
tok = name
|
||||
} else {
|
||||
tok = IDENT
|
||||
}
|
||||
case isDigit(ch):
|
||||
tok = NUMBER
|
||||
lit, err = s.scanNumber()
|
||||
|
@ -140,58 +107,17 @@ retry:
|
|||
lit = string(ch)
|
||||
}
|
||||
case '+':
|
||||
s.next()
|
||||
switch s.peek() {
|
||||
case '+':
|
||||
tok = PLUSPLUS
|
||||
lit = "++"
|
||||
case '=':
|
||||
tok = PLUSEQ
|
||||
lit = "+="
|
||||
default:
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
case '-':
|
||||
s.next()
|
||||
switch s.peek() {
|
||||
case '-':
|
||||
tok = MINUSMINUS
|
||||
lit = "--"
|
||||
case '=':
|
||||
tok = MINUSEQ
|
||||
lit = "-="
|
||||
default:
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
case '*':
|
||||
s.next()
|
||||
switch s.peek() {
|
||||
case '*':
|
||||
tok = POW
|
||||
lit = "**"
|
||||
case '=':
|
||||
tok = MULEQ
|
||||
lit = "*="
|
||||
default:
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
case '/':
|
||||
s.next()
|
||||
switch s.peek() {
|
||||
case '=':
|
||||
tok = DIVEQ
|
||||
lit = "/="
|
||||
default:
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
case '>':
|
||||
s.next()
|
||||
switch s.peek() {
|
||||
|
@ -209,9 +135,6 @@ retry:
|
|||
case '<':
|
||||
s.next()
|
||||
switch s.peek() {
|
||||
case '-':
|
||||
tok = OPCHAN
|
||||
lit = "<-"
|
||||
case '=':
|
||||
tok = LE
|
||||
lit = "<="
|
||||
|
@ -229,9 +152,6 @@ retry:
|
|||
case '|':
|
||||
tok = OROR
|
||||
lit = "||"
|
||||
case '=':
|
||||
tok = OREQ
|
||||
lit = "|="
|
||||
default:
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
|
@ -243,50 +163,24 @@ retry:
|
|||
case '&':
|
||||
tok = ANDAND
|
||||
lit = "&&"
|
||||
case '=':
|
||||
tok = ANDEQ
|
||||
lit = "&="
|
||||
default:
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
case '.':
|
||||
s.next()
|
||||
if s.peek() == '.' {
|
||||
s.next()
|
||||
if s.peek() == '.' {
|
||||
tok = VARARG
|
||||
} else {
|
||||
err = fmt.Errorf(`syntax error "%s"`, "..")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
case '(', ')', ':', ';', '%', '?', '{', '}', ',', '[', ']', '^', '\n':
|
||||
s.next()
|
||||
if ch == '[' && s.peek() == ']' {
|
||||
s.next()
|
||||
if isLetter(s.peek()) {
|
||||
s.back()
|
||||
tok = ARRAYLIT
|
||||
lit = "[]"
|
||||
} else {
|
||||
s.back()
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
} else {
|
||||
s.back()
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
}
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
case '\n':
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
case '(', ')', ':', ';', '%', '?', '{', '}', ',', '[', ']', '^':
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
default:
|
||||
err = fmt.Errorf(`syntax error "%s"`, string(ch))
|
||||
tok = int(ch)
|
||||
lit = string(ch)
|
||||
return
|
||||
}
|
||||
s.next()
|
||||
|
@ -296,7 +190,7 @@ retry:
|
|||
|
||||
// isLetter returns true if the rune is a letter for identity.
|
||||
func isLetter(ch rune) bool {
|
||||
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
|
||||
return unicode.IsLetter(ch) || ch == '_'
|
||||
}
|
||||
|
||||
// isDigit returns true if the rune is a number.
|
||||
|
@ -498,10 +392,9 @@ type Lexer struct {
|
|||
func (l *Lexer) Lex(lval *yySymType) int {
|
||||
tok, lit, pos, err := l.s.Scan()
|
||||
if err != nil {
|
||||
l.e = &Error{Message: fmt.Sprintf("%s", err.Error()), Pos: pos, Fatal: true}
|
||||
l.e = &Error{Message: fmt.Sprintf("%s", err.Error()), Fatal: true}
|
||||
}
|
||||
lval.tok = ast.Token{Tok: tok, Lit: lit}
|
||||
lval.tok.SetPosition(pos)
|
||||
l.lit = lit
|
||||
l.pos = pos
|
||||
return tok
|
||||
|
@ -509,7 +402,7 @@ func (l *Lexer) Lex(lval *yySymType) int {
|
|||
|
||||
// Error sets parse error.
|
||||
func (l *Lexer) Error(msg string) {
|
||||
l.e = &Error{Message: msg, Pos: l.pos, Fatal: false}
|
||||
l.e = &Error{Message: msg, Fatal: false}
|
||||
}
|
||||
|
||||
// Parser provides way to parse the code using Scanner.
|
||||
|
@ -521,6 +414,10 @@ func Parse(s *Scanner) ([]ast.Stmt, error) {
|
|||
return l.stmts, l.e
|
||||
}
|
||||
|
||||
func EnableErrorVerbose() {
|
||||
yyErrorVerbose = true
|
||||
}
|
||||
|
||||
// ParserSrc provides way to parse the code from source.
|
||||
func ParseSrc(src string) ([]ast.Stmt, error) {
|
||||
scanner := &Scanner{
|
752
vendor/github.com/mattn/kinako/parser/parser.go
generated
vendored
Normal file
752
vendor/github.com/mattn/kinako/parser/parser.go
generated
vendored
Normal file
|
@ -0,0 +1,752 @@
|
|||
//line parser.go.y:2
|
||||
package parser
|
||||
|
||||
import __yyfmt__ "fmt"
|
||||
|
||||
//line parser.go.y:2
|
||||
import (
|
||||
"github.com/mattn/kinako/ast"
|
||||
)
|
||||
|
||||
//line parser.go.y:15
|
||||
type yySymType struct {
|
||||
yys int
|
||||
compstmt []ast.Stmt
|
||||
stmts []ast.Stmt
|
||||
stmt ast.Stmt
|
||||
expr ast.Expr
|
||||
tok ast.Token
|
||||
term ast.Token
|
||||
terms ast.Token
|
||||
opt_terms ast.Token
|
||||
}
|
||||
|
||||
const IDENT = 57346
|
||||
const NUMBER = 57347
|
||||
const STRING = 57348
|
||||
const EQEQ = 57349
|
||||
const NEQ = 57350
|
||||
const GE = 57351
|
||||
const LE = 57352
|
||||
const OROR = 57353
|
||||
const ANDAND = 57354
|
||||
const POW = 57355
|
||||
const SHIFTLEFT = 57356
|
||||
const SHIFTRIGHT = 57357
|
||||
const PLUSPLUS = 57358
|
||||
const MINUSMINUS = 57359
|
||||
const UNARY = 57360
|
||||
|
||||
var yyToknames = [...]string{
|
||||
"$end",
|
||||
"error",
|
||||
"$unk",
|
||||
"IDENT",
|
||||
"NUMBER",
|
||||
"STRING",
|
||||
"EQEQ",
|
||||
"NEQ",
|
||||
"GE",
|
||||
"LE",
|
||||
"OROR",
|
||||
"ANDAND",
|
||||
"POW",
|
||||
"'='",
|
||||
"'?'",
|
||||
"':'",
|
||||
"','",
|
||||
"'>'",
|
||||
"'<'",
|
||||
"SHIFTLEFT",
|
||||
"SHIFTRIGHT",
|
||||
"'+'",
|
||||
"'-'",
|
||||
"PLUSPLUS",
|
||||
"MINUSMINUS",
|
||||
"'*'",
|
||||
"'/'",
|
||||
"'%'",
|
||||
"UNARY",
|
||||
"'!'",
|
||||
"'^'",
|
||||
"'('",
|
||||
"')'",
|
||||
"'|'",
|
||||
"'&'",
|
||||
"';'",
|
||||
"'\\n'",
|
||||
}
|
||||
var yyStatenames = [...]string{}
|
||||
|
||||
const yyEofCode = 1
|
||||
const yyErrCode = 2
|
||||
const yyInitialStackSize = 16
|
||||
|
||||
//line parser.go.y:194
|
||||
|
||||
//line yacctab:1
|
||||
var yyExca = [...]int{
|
||||
-1, 1,
|
||||
1, -1,
|
||||
-2, 0,
|
||||
-1, 49,
|
||||
7, 0,
|
||||
8, 0,
|
||||
-2, 20,
|
||||
-1, 50,
|
||||
7, 0,
|
||||
8, 0,
|
||||
-2, 21,
|
||||
}
|
||||
|
||||
const yyNprod = 36
|
||||
const yyPrivate = 57344
|
||||
|
||||
var yyTokenNames []string
|
||||
var yyStates []string
|
||||
|
||||
const yyLast = 249
|
||||
|
||||
var yyAct = [...]int{
|
||||
|
||||
9, 6, 7, 33, 35, 4, 22, 23, 3, 18,
|
||||
24, 25, 26, 37, 38, 39, 2, 40, 33, 35,
|
||||
17, 42, 43, 44, 45, 46, 47, 48, 49, 50,
|
||||
51, 52, 53, 54, 55, 56, 57, 58, 27, 28,
|
||||
30, 32, 34, 36, 8, 1, 21, 60, 0, 29,
|
||||
31, 0, 0, 22, 23, 5, 0, 24, 25, 26,
|
||||
19, 61, 0, 41, 0, 33, 35, 27, 28, 30,
|
||||
32, 34, 36, 0, 19, 21, 0, 0, 29, 31,
|
||||
0, 0, 22, 23, 0, 0, 24, 25, 26, 0,
|
||||
0, 0, 0, 59, 33, 35, 27, 28, 30, 32,
|
||||
34, 36, 0, 20, 21, 0, 0, 29 |