// Code generated by goyacc -o tiny.y.go -p yy tiny.y. DO NOT EDIT.

//line tiny.y:12
package parser

import __yyfmt__ "fmt"

//line tiny.y:12

import (
	"fmt"
	"strconv"

	"github.com/chai2010/tinylang/tiny/ast"
	"github.com/chai2010/tinylang/tiny/token"
)

var _ = fmt.Sprint

//line tiny.y:25
type yySymType struct {
	yys int
	ctx struct {
		file     *ast.File
		expr     ast.Expr
		stmt     ast.Stmt
		stmtList []ast.Stmt
		node     ast.Node

		pos token.Pos
		tok token.Token
		lit string
	}
}

const _IDENT = 57346
const _NUMBER = 57347
const _EQ = 57348
const _LT = 57349
const _PLUS = 57350
const _MINUS = 57351
const _TIMES = 57352
const _OVER = 57353
const _ASSIGN = 57354
const _IF = 57355
const _THEN = 57356
const _ELSE = 57357
const _END = 57358
const _REPEAT = 57359
const _UNTIL = 57360
const _READ = 57361
const _WRITE = 57362
const _LPAREN = 57363
const _RPAREN = 57364
const _SEMI = 57365

var yyToknames = [...]string{
	"$end",
	"error",
	"$unk",
	"_IDENT",
	"_NUMBER",
	"_EQ",
	"_LT",
	"_PLUS",
	"_MINUS",
	"_TIMES",
	"_OVER",
	"_ASSIGN",
	"_IF",
	"_THEN",
	"_ELSE",
	"_END",
	"_REPEAT",
	"_UNTIL",
	"_READ",
	"_WRITE",
	"_LPAREN",
	"_RPAREN",
	"_SEMI",
}
var yyStatenames = [...]string{}

const yyEofCode = 1
const yyErrCode = 2
const yyInitialStackSize = 16

//line tiny.y:297

//line yacctab:1
var yyExca = [...]int{
	-1, 0,
	23, 10,
	-2, 2,
	-1, 1,
	1, -1,
	-2, 0,
}

const yyPrivate = 57344

const yyLast = 54

var yyAct = [...]int{

	2, 15, 18, 16, 47, 46, 49, 17, 14, 21,
	20, 22, 14, 14, 11, 25, 44, 35, 27, 3,
	23, 34, 14, 9, 24, 36, 19, 10, 37, 12,
	13, 8, 38, 39, 26, 42, 43, 45, 40, 41,
	32, 33, 29, 28, 30, 31, 30, 31, 48, 7,
	5, 4, 6, 1,
}
var yyPact = [...]int{

	10, -1000, -15, -1000, -1000, -1000, -1000, -1000, -1000, 5,
	10, 8, 20, 5, 10, 4, 36, 30, -1000, 5,
	-1000, -1000, -1, 5, -1000, -1000, -1000, 10, 5, 5,
	5, 5, 5, 5, -6, 5, -1000, -11, 38, 38,
	30, 30, -1000, -1000, -1000, -1000, -1000, 10, -10, -1000,
}
var yyPgo = [...]int{

	0, 53, 2, 1, 7, 3, 52, 51, 50, 49,
	31, 19, 0,
}
var yyR1 = [...]int{

	0, 1, 1, 12, 12, 11, 11, 11, 11, 11,
	11, 7, 7, 8, 6, 9, 10, 3, 3, 3,
	5, 5, 5, 4, 4, 4, 2, 2, 2,
}
var yyR2 = [...]int{

	0, 1, 0, 3, 1, 1, 1, 1, 1, 1,
	0, 5, 7, 4, 3, 2, 2, 3, 3, 1,
	3, 3, 1, 3, 3, 1, 3, 1, 1,
}
var yyChk = [...]int{

	-1000, -1, -12, -11, -7, -8, -6, -9, -10, 13,
	17, 4, 19, 20, 23, -3, -5, -4, -2, 21,
	5, 4, -12, 12, 4, -3, -11, 14, 7, 6,
	8, 9, 10, 11, -3, 18, -3, -12, -5, -5,
	-4, -4, -2, -2, 22, -3, 16, 15, -12, 16,
}
var yyDef = [...]int{

	-2, -2, 1, 4, 5, 6, 7, 8, 9, 0,
	10, 0, 0, 0, 10, 0, 19, 22, 25, 0,
	27, 28, 0, 0, 15, 16, 3, 10, 0, 0,
	0, 0, 0, 0, 0, 0, 14, 0, 17, 18,
	20, 21, 23, 24, 26, 13, 11, 10, 0, 12,
}
var yyTok1 = [...]int{

	1,
}
var yyTok2 = [...]int{

	2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
	12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
	22, 23,
}
var yyTok3 = [...]int{
	0,
}

var yyErrorMessages = [...]struct {
	state int
	token int
	msg   string
}{}

//line yaccpar:1

/*	parser for yacc output	*/

var (
	yyDebug        = 0
	yyErrorVerbose = false
)

type yyLexer interface {
	Lex(lval *yySymType) int
	Error(s string)
}

type yyParser interface {
	Parse(yyLexer) int
	Lookahead() int
}

type yyParserImpl struct {
	lval  yySymType
	stack [yyInitialStackSize]yySymType
	char  int
}

func (p *yyParserImpl) Lookahead() int {
	return p.char
}

func yyNewParser() yyParser {
	return &yyParserImpl{}
}

const yyFlag = -1000

func yyTokname(c int) string {
	if c >= 1 && c-1 < len(yyToknames) {
		if yyToknames[c-1] != "" {
			return yyToknames[c-1]
		}
	}
	return __yyfmt__.Sprintf("tok-%v", c)
}

func yyStatname(s int) string {
	if s >= 0 && s < len(yyStatenames) {
		if yyStatenames[s] != "" {
			return yyStatenames[s]
		}
	}
	return __yyfmt__.Sprintf("state-%v", s)
}

func yyErrorMessage(state, lookAhead int) string {
	const TOKSTART = 4

	if !yyErrorVerbose {
		return "syntax error"
	}

	for _, e := range yyErrorMessages {
		if e.state == state && e.token == lookAhead {
			return "syntax error: " + e.msg
		}
	}

	res := "syntax error: unexpected " + yyTokname(lookAhead)

	// To match Bison, suggest at most four expected tokens.
	expected := make([]int, 0, 4)

	// Look for shiftable tokens.
	base := yyPact[state]
	for tok := TOKSTART; tok-1 < len(yyToknames); tok++ {
		if n := base + tok; n >= 0 && n < yyLast && yyChk[yyAct[n]] == tok {
			if len(expected) == cap(expected) {
				return res
			}
			expected = append(expected, tok)
		}
	}

	if yyDef[state] == -2 {
		i := 0
		for yyExca[i] != -1 || yyExca[i+1] != state {
			i += 2
		}

		// Look for tokens that we accept or reduce.
		for i += 2; yyExca[i] >= 0; i += 2 {
			tok := yyExca[i]
			if tok < TOKSTART || yyExca[i+1] == 0 {
				continue
			}
			if len(expected) == cap(expected) {
				return res
			}
			expected = append(expected, tok)
		}

		// If the default action is to accept or reduce, give up.
		if yyExca[i+1] != 0 {
			return res
		}
	}

	for i, tok := range expected {
		if i == 0 {
			res += ", expecting "
		} else {
			res += " or "
		}
		res += yyTokname(tok)
	}
	return res
}

func yylex1(lex yyLexer, lval *yySymType) (char, token int) {
	token = 0
	char = lex.Lex(lval)
	if char <= 0 {
		token = yyTok1[0]
		goto out
	}
	if char < len(yyTok1) {
		token = yyTok1[char]
		goto out
	}
	if char >= yyPrivate {
		if char < yyPrivate+len(yyTok2) {
			token = yyTok2[char-yyPrivate]
			goto out
		}
	}
	for i := 0; i < len(yyTok3); i += 2 {
		token = yyTok3[i+0]
		if token == char {
			token = yyTok3[i+1]
			goto out
		}
	}

out:
	if token == 0 {
		token = yyTok2[1] /* unknown char */
	}
	if yyDebug >= 3 {
		__yyfmt__.Printf("lex %s(%d)\n", yyTokname(token), uint(char))
	}
	return char, token
}

func yyParse(yylex yyLexer) int {
	return yyNewParser().Parse(yylex)
}

func (yyrcvr *yyParserImpl) Parse(yylex yyLexer) int {
	var yyn int
	var yyVAL yySymType
	var yyDollar []yySymType
	_ = yyDollar // silence set and not used
	yyS := yyrcvr.stack[:]

	Nerrs := 0   /* number of errors */
	Errflag := 0 /* error recovery flag */
	yystate := 0
	yyrcvr.char = -1
	yytoken := -1 // yyrcvr.char translated into internal numbering
	defer func() {
		// Make sure we report no lookahead when not parsing.
		yystate = -1
		yyrcvr.char = -1
		yytoken = -1
	}()
	yyp := -1
	goto yystack

ret0:
	return 0

ret1:
	return 1

yystack:
	/* put a state and value onto the stack */
	if yyDebug >= 4 {
		__yyfmt__.Printf("char %v in %v\n", yyTokname(yytoken), yyStatname(yystate))
	}

	yyp++
	if yyp >= len(yyS) {
		nyys := make([]yySymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
	}
	yyS[yyp] = yyVAL
	yyS[yyp].yys = yystate

yynewstate:
	yyn = yyPact[yystate]
	if yyn <= yyFlag {
		goto yydefault /* simple state */
	}
	if yyrcvr.char < 0 {
		yyrcvr.char, yytoken = yylex1(yylex, &yyrcvr.lval)
	}
	yyn += yytoken
	if yyn < 0 || yyn >= yyLast {
		goto yydefault
	}
	yyn = yyAct[yyn]
	if yyChk[yyn] == yytoken { /* valid shift */
		yyrcvr.char = -1
		yytoken = -1
		yyVAL = yyrcvr.lval
		yystate = yyn
		if Errflag > 0 {
			Errflag--
		}
		goto yystack
	}

yydefault:
	/* default state action */
	yyn = yyDef[yystate]
	if yyn == -2 {
		if yyrcvr.char < 0 {
			yyrcvr.char, yytoken = yylex1(yylex, &yyrcvr.lval)
		}

		/* look through exception table */
		xi := 0
		for {
			if yyExca[xi+0] == -1 && yyExca[xi+1] == yystate {
				break
			}
			xi += 2
		}
		for xi += 2; ; xi += 2 {
			yyn = yyExca[xi+0]
			if yyn < 0 || yyn == yytoken {
				break
			}
		}
		yyn = yyExca[xi+1]
		if yyn < 0 {
			goto ret0
		}
	}
	if yyn == 0 {
		/* error ... attempt to resume parsing */
		switch Errflag {
		case 0: /* brand new error */
			yylex.Error(yyErrorMessage(yystate, yytoken))
			Nerrs++
			if yyDebug >= 1 {
				__yyfmt__.Printf("%s", yyStatname(yystate))
				__yyfmt__.Printf(" saw %s\n", yyTokname(yytoken))
			}
			fallthrough

		case 1, 2: /* incompletely recovered error ... try again */
			Errflag = 3

			/* find a state where "error" is a legal shift action */
			for yyp >= 0 {
				yyn = yyPact[yyS[yyp].yys] + yyErrCode
				if yyn >= 0 && yyn < yyLast {
					yystate = yyAct[yyn] /* simulate a shift of "error" */
					if yyChk[yystate] == yyErrCode {
						goto yystack
					}
				}

				/* the current p has no shift on "error", pop stack */
				if yyDebug >= 2 {
					__yyfmt__.Printf("error recovery pops state %d\n", yyS[yyp].yys)
				}
				yyp--
			}
			/* there is no state on the stack with an error shift ... abort */
			goto ret1

		case 3: /* no shift yet; clobber input char */
			if yyDebug >= 2 {
				__yyfmt__.Printf("error recovery discards %s\n", yyTokname(yytoken))
			}
			if yytoken == yyEofCode {
				goto ret1
			}
			yyrcvr.char = -1
			yytoken = -1
			goto yynewstate /* try again in the same state */
		}
	}

	/* reduction by production yyn */
	if yyDebug >= 2 {
		__yyfmt__.Printf("reduce %v in:\n\t%v\n", yyn, yyStatname(yystate))
	}

	yynt := yyn
	yypt := yyp
	_ = yypt // guard against "declared and not used"

	yyp -= yyR2[yyn]
	// yyp is now the index of $0. Perform the default action. Iff the
	// reduced production is ε, $1 is possibly out of range.
	if yyp+1 >= len(yyS) {
		nyys := make([]yySymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
	}
	yyVAL = yyS[yyp+1]

	/* consult goto table to find next state */
	yyn = yyR1[yyn]
	yyg := yyPgo[yyn]
	yyj := yyg + yyS[yyp].yys + 1

	if yyj >= yyLast {
		yystate = yyAct[yyg]
	} else {
		yystate = yyAct[yyj]
		if yyChk[yystate] != -yyn {
			yystate = yyAct[yyg]
		}
	}
	// dummy call; replaced with literal code
	switch yynt {

	case 1:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:70
		{
			yyDebugln("program.stmt_seq end")
			yyVAL.ctx.file = &ast.File{
				List: yyDollar[1].ctx.stmtList,
			}

			// https://github.com/golang/tools/commit/a965a571dd795205ab6582d15ea92e1350374b58
			//
			// Code inside the grammar actions may refer to yyrcvr,
			// which holds the yyParser.
			yyrcvr.lval.ctx.file = yyVAL.ctx.file
		}
	case 2:
		yyDollar = yyS[yypt-0 : yypt+1]
//line tiny.y:82
		{
			yyDebugln("program.<empty>")
			yyVAL.ctx.file = &ast.File{}
		}
	case 3:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:88
		{
			yyDebugln("stmt_seq.stmt_seq SEMI stmt")
			yyVAL.ctx.stmtList = append(yyVAL.ctx.stmtList, yyDollar[3].ctx.stmt)
		}
	case 4:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:92
		{
			yyDebugln("stmt_seq.stmt")
			if yyDollar[1].ctx.stmt != nil {
				yyVAL.ctx.stmtList = []ast.Stmt{yyDollar[1].ctx.stmt}
			}
		}
	case 5:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:100
		{
			yyDebugln("stmt.if_stmt")
			yyVAL.ctx.stmt = yyDollar[1].ctx.stmt
		}
	case 6:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:104
		{
			yyDebugln("stmt.repeat_stmt")
			yyVAL.ctx.stmt = yyDollar[1].ctx.stmt
		}
	case 7:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:108
		{
			yyDebugln("stmt.assign_stmt")
			yyVAL.ctx.stmt = yyDollar[1].ctx.stmt
		}
	case 8:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:112
		{
			yyDebugln("stmt.read_stmt end")
			yyVAL.ctx.stmt = yyDollar[1].ctx.stmt
		}
	case 9:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:116
		{
			yyDebugln("stmt.write_stmt")
			yyVAL.ctx.stmt = yyDollar[1].ctx.stmt
		}
	case 10:
		yyDollar = yyS[yypt-0 : yypt+1]
//line tiny.y:120
		{
			yyVAL.ctx.stmt = nil
		}
	case 11:
		yyDollar = yyS[yypt-5 : yypt+1]
//line tiny.y:125
		{
			yyDebugln("if_stmt.IF exp THEN stmt_seq END")
			yyVAL.ctx.stmt = &ast.IfStmt{
				If:   yyDollar[1].ctx.pos,
				Cond: yyDollar[2].ctx.expr,
				Body: &ast.BlockStmt{
					List: yyDollar[4].ctx.stmtList,
				},
			}
		}
	case 12:
		yyDollar = yyS[yypt-7 : yypt+1]
//line tiny.y:135
		{
			yyDebugln("if_stmt.IF exp THEN stmt_seq ELSE stmt_seq END")
			yyVAL.ctx.stmt = &ast.IfStmt{
				If:   yyDollar[1].ctx.pos,
				Cond: yyDollar[2].ctx.expr,
				Body: &ast.BlockStmt{
					List: yyDollar[4].ctx.stmtList,
				},
				Else: &ast.BlockStmt{
					List: yyDollar[6].ctx.stmtList,
				},
			}
		}
	case 13:
		yyDollar = yyS[yypt-4 : yypt+1]
//line tiny.y:150
		{
			yyDebugln("repeat_stmt.REPEAT stmt_seq UNTIL exp")
			yyVAL.ctx.stmt = &ast.RepeatStmt{
				Repeat: yyDollar[1].ctx.pos,
				Until:  yyDollar[4].ctx.expr,
				Body: &ast.BlockStmt{
					List: yyDollar[2].ctx.stmtList,
				},
			}
		}
	case 14:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:162
		{
			yyDebugln("assign_stmt.ASSIGN exp")
			yyVAL.ctx.stmt = &ast.AssignStmt{
				Target: &ast.Ident{
					NamePos: yyDollar[1].ctx.pos,
					Name:    yyDollar[1].ctx.lit,
				},
				TokPos: yyDollar[2].ctx.pos,
				Value:  yyDollar[3].ctx.expr,
			}
		}
	case 15:
		yyDollar = yyS[yypt-2 : yypt+1]
//line tiny.y:175
		{
			yyDebugln("read_stmt.READ ID")
			yyVAL.ctx.stmt = &ast.ReadStmt{
				Read: yyDollar[1].ctx.pos,
				Target: &ast.Ident{
					NamePos: yyDollar[2].ctx.pos,
					Name:    yyDollar[2].ctx.lit,
				},
			}
		}
	case 16:
		yyDollar = yyS[yypt-2 : yypt+1]
//line tiny.y:187
		{
			yyDebugln("write_stmt.WRITE exp")
			yyVAL.ctx.stmt = &ast.WriteStmt{
				Write: yyDollar[1].ctx.pos,
				Value: yyDollar[2].ctx.expr,
			}
		}
	case 17:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:196
		{
			yyDebugln("exp.simple_exp LT simple_exp")
			yyVAL.ctx.expr = &ast.BinaryExpr{
				X:     yyDollar[1].ctx.expr,
				OpPos: yyDollar[2].ctx.pos,
				Op:    yyDollar[2].ctx.tok,
				Y:     yyDollar[3].ctx.expr,
			}
		}
	case 18:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:205
		{
			yyDebugln("exp.simple_exp EQ simple_exp")
			yyVAL.ctx.expr = &ast.BinaryExpr{
				X:     yyDollar[1].ctx.expr,
				OpPos: yyDollar[2].ctx.pos,
				Op:    yyDollar[2].ctx.tok,
				Y:     yyDollar[3].ctx.expr,
			}
		}
	case 19:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:214
		{
			yyDebugln("exp.simple_exp")
			yyVAL.ctx.expr = yyDollar[1].ctx.expr
		}
	case 20:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:220
		{
			yyDebugln("simple_exp.simple_exp PLUS term")
			yyVAL.ctx.expr = &ast.BinaryExpr{
				X:     yyDollar[1].ctx.expr,
				OpPos: yyDollar[2].ctx.pos,
				Op:    yyDollar[2].ctx.tok,
				Y:     yyDollar[3].ctx.expr,
			}
		}
	case 21:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:229
		{
			yyDebugln("simple_exp.simple_exp MINUS term")
			yyVAL.ctx.expr = &ast.BinaryExpr{
				X:     yyDollar[1].ctx.expr,
				OpPos: yyDollar[2].ctx.pos,
				Op:    yyDollar[2].ctx.tok,
				Y:     yyDollar[3].ctx.expr,
			}
		}
	case 22:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:238
		{
			yyDebugln("simple_exp.term")
			yyVAL.ctx.expr = yyDollar[1].ctx.expr
		}
	case 23:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:244
		{
			yyDebugln("term.term TIMES factor")
			yyVAL.ctx.expr = &ast.BinaryExpr{
				X:     yyDollar[1].ctx.expr,
				OpPos: yyDollar[2].ctx.pos,
				Op:    yyDollar[2].ctx.tok,
				Y:     yyDollar[3].ctx.expr,
			}
		}
	case 24:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:253
		{
			yyDebugln("term.term OVER factor")
			yyVAL.ctx.expr = &ast.BinaryExpr{
				X:     yyDollar[1].ctx.expr,
				OpPos: yyDollar[2].ctx.pos,
				Op:    yyDollar[2].ctx.tok,
				Y:     yyDollar[3].ctx.expr,
			}
		}
	case 25:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:262
		{
			yyDebugln("term.factor")
			yyVAL.ctx.expr = yyDollar[1].ctx.expr
		}
	case 26:
		yyDollar = yyS[yypt-3 : yypt+1]
//line tiny.y:268
		{
			yyDebugln("factor.LPAREN exp RPAREN")
			yyVAL.ctx.expr = &ast.ParenExpr{
				Lparen: yyDollar[1].ctx.pos,
				X:      yyDollar[2].ctx.expr,
				Rparen: yyDollar[3].ctx.pos,
			}
		}
	case 27:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:276
		{
			yyDebugln("factor.NUM")
			v, err := strconv.Atoi(yyDollar[1].ctx.lit)
			if err != nil {
				yylex.Error("aaa")
				return 0
			}
			yyVAL.ctx.expr = &ast.Number{
				ValuePos: yyDollar[1].ctx.pos,
				ValueEnd: yyDollar[1].ctx.pos + token.Pos(len(yyDollar[1].ctx.lit)),
				Value:    v,
			}
		}
	case 28:
		yyDollar = yyS[yypt-1 : yypt+1]
//line tiny.y:289
		{
			yyDebugln("factor.ID")
			yyVAL.ctx.expr = &ast.Ident{
				NamePos: yyDollar[1].ctx.pos,
				Name:    yyDollar[1].ctx.lit,
			}
		}
	}
	goto yystack /* stack new state and value */
}
