# How to use resetState method of parser Package

Best Gauge code snippet using parser.resetState

parsing.go

Source:parsing.go

`...77 if len(p.states) != 0 {78 p.currentState = p.states[len(p.states)-1]79 }80}81func (p *Parser) resetState() {82 if len(p.states) != 0 {83 p.states = p.states[:len(p.states)-1]84 if len(p.states) != 0 {85 p.currentState = p.states[len(p.states)-1]86 } else {87 p.currentState = NoneParsed88 }89 }90}91func (p *Parser) auxiliary(auxiliaries []Symbol) []Symbol {92 if p.tokens[p.currentToken].IsAuxiliary() {93 auxiliaries = append(auxiliaries, p.tokens[p.currentToken])94 p.currentToken++95 auxiliaries = p.auxiliary(auxiliaries)96 return auxiliaries97 } else {98 return auxiliaries99 }100}101func (p *Parser) open() bool {102 p.setState()103 if p.tokens[p.currentToken].IsEnclosingOperation() {104 p.currentToken++105 return true106 } else {107 return false108 }109}110func (p *Parser) atom() int {111 if p.tokens[p.currentToken].SymbolType == Variable || p.tokens[p.currentToken].SymbolType == Constant {112 child := p.addNode()113 p.currentToken++114 return child115 }116 return -1117}118func (p *Parser) operand() int {119 auxiliaries := p.auxiliary(make([]Symbol, 0))120 // these types all need to be mutually exclusive121 atom := p.atom()122 if atom != -1 {123 p.addAuxiliaries(atom, auxiliaries)124 return atom125 }126 // set := p.set()127 // if set != -1 {128 // p.addAuxiliaries(set, auxiliaries)129 // return set130 // }131 // list := p.list()132 // if list != -1 {133 // p.addAuxiliaries(list, auxiliaries)134 // return list135 // }136 function := p.function()137 if function != -1 {138 p.addAuxiliaries(function, auxiliaries)139 return function140 }141 subExpression := p.expression()142 if subExpression != -1 {143 p.addAuxiliaries(subExpression, auxiliaries)144 return subExpression145 }146 return -1147}148func (p *Parser) operands(parent int, children []int) (int, []int) {149 if p.close() { // used directly after first operand, i.e. the lhs, so if atomic will close150 return parent, children151 } else {152 if p.tokens[p.currentToken].IsOperation() {153 // if p.tokens[p.currentToken].SymbolType == Iteration && p.currentState == SubexpressionParsed {154 // p.currentState = NaryTupleParsed155 // }156 if parent == -1 {157 parent = p.addNode()158 }159 if p.tokens[p.currentToken].SymbolType != Subtraction {160 p.currentToken++161 }162 }163 children = append(children, p.operand())164 parent, children = p.operands(parent, children)165 return parent, children166 }167}168func (p *Parser) close() bool {169 if p.tokens[p.currentToken].ClosesExpressionScope() {170 if p.tokens[p.currentToken].SymbolType != ExpressionClose {171 p.resetState()172 }173 p.currentToken++174 return true175 } else if p.tokens[p.currentToken].SymbolType == NewLine || p.tokens[p.currentToken].SymbolType == EndOfFile {176 return true177 } else {178 return false179 }180}181func (p *Parser) expression() int {182 subExpressions := make([]int, 0)183 p.open() // optional open for negatives and subexpressions184 first := p.operand()185 parent, children := p.operands(-1, append(subExpressions, first))...`

testing.go

Source:testing.go

`1package expreduce2import (3 "bytes"4 "math"5 "testing"6 "github.com/corywalker/expreduce/expreduce/atoms"7 "github.com/corywalker/expreduce/expreduce/parser"8 "github.com/corywalker/expreduce/pkg/expreduceapi"9 "github.com/stretchr/testify/assert"10)11type testDesc struct {12 module string13 def Definition14 desc string15}16type TestInstruction interface {17 run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool18}19type SameTest struct {20 Out string21 In string22}23func (test *SameTest) run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool {24 return casAssertDescSame(t, es, test.Out, test.In, td.desc)25}26type StringTest struct {27 Out string28 In string29}30func (test *StringTest) run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool {31 return assert.Equal(t, test.Out, EasyRun(test.In, es), td.desc)32}33type ExampleOnlyInstruction struct {34 Out string35 In string36}37func (test *ExampleOnlyInstruction) run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool {38 return true39}40type ResetState struct{}41func (test *ResetState) run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool {42 es.ClearAll()43 return true44}45type TestComment struct {46 Comment string47}48func (test *TestComment) run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool {49 return true50}51type SameTestEx struct {52 Out expreduceapi.Ex53 In expreduceapi.Ex54 // The pecent difference we are willing to tolerate.55 floatTolerance float6456}57func (test *SameTestEx) run(t *testing.T, es expreduceapi.EvalStateInterface, td testDesc) bool {58 stringParams := ActualStringFormArgsFull("InputForm", es)59 succ, s := casTestInner(es, es.Eval(test.In), es.Eval(test.Out), test.In.StringForm(stringParams), true, td.desc, test.floatTolerance)60 assert.True(t, succ, s)61 return succ62}63func casTestInner(es expreduceapi.EvalStateInterface, inTree expreduceapi.Ex, outTree expreduceapi.Ex, inStr string, test bool, desc string, floatTolerance float64) (succ bool, s string) {64 context, contextPath := definitionComplexityStringFormArgs()65 var buffer bytes.Buffer66 buffer.WriteString("(")67 buffer.WriteString(inTree.StringForm(expreduceapi.ToStringParams{Form: "InputForm", Context: context, ContextPath: contextPath, Esi: es}))68 if test {69 buffer.WriteString(") != (")70 } else {71 buffer.WriteString(") == (")72 }73 buffer.WriteString(outTree.StringForm(expreduceapi.ToStringParams{Form: "InputForm", Context: context, ContextPath: contextPath, Esi: es}))74 buffer.WriteString(")")75 buffer.WriteString("\n\tInput was: ")76 buffer.WriteString(inStr)77 if len(desc) != 0 {78 buffer.WriteString("\n\tDesc: ")79 buffer.WriteString(desc)80 }81 outFloat, outIsFloat := outTree.(*atoms.Flt)82 inFloat, inIsFloat := inTree.(*atoms.Flt)83 if floatTolerance > 0 && outIsFloat && inIsFloat {84 if outFloat.Val.Sign() == inFloat.Val.Sign() && outFloat.Val.Sign() != 0 {85 inVal, _ := inFloat.Val.Float64()86 outVal, _ := outFloat.Val.Float64()87 pctDiff := (inVal - outVal) / ((inVal + outVal) / 2)88 pctDiff = math.Abs(pctDiff) * 10089 return pctDiff < floatTolerance, buffer.String()90 }91 }92 theTestTree := atoms.NewExpression([]expreduceapi.Ex{93 atoms.NewSymbol("System`SameQ"),94 atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`Hold"), inTree}),95 atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`Hold"), outTree}),96 })97 theTest := es.Eval(theTestTree)98 resSym, resIsSym := theTest.(*atoms.Symbol)99 if !resIsSym {100 return false, buffer.String()101 }102 if test {103 return resSym.Name == "System`True", buffer.String()104 }105 return resSym.Name == "System`False", buffer.String()106}107func casAssertSame(t *testing.T, es expreduceapi.EvalStateInterface, out string, in string) bool {108 succ, s := casTestInner(es, es.Eval(parser.Interp(in, es)), es.Eval(parser.Interp(out, es)), in, true, "", 0)109 assert.True(t, succ, s)110 return succ111}112func casAssertDiff(t *testing.T, es expreduceapi.EvalStateInterface, out string, in string) bool {113 succ, s := casTestInner(es, es.Eval(parser.Interp(in, es)), es.Eval(parser.Interp(out, es)), in, false, "", 0)114 assert.True(t, succ, s)115 return succ116}117func casAssertDescSame(t *testing.T, es expreduceapi.EvalStateInterface, out string, in string, desc string) bool {118 succ, s := casTestInner(es, es.Eval(parser.Interp(in, es)), es.Eval(parser.Interp(out, es)), in, true, desc, 0)119 assert.True(t, succ, s)120 return succ121}...`

parse.go

Source:parse.go

`...134 switch v.(type) {135 case []interface{}, map[string]interface{}:136 p.push(v)137 default:138 p.resetState()139 }140}141func (p *parser) push(v interface{}) {142 p.stack = append(p.stack, v)143 p.container = v144 switch v.(type) {145 case []interface{}:146 p.state = stateBeforeArrayElement147 case map[string]interface{}:148 p.state = stateBeforeObjectKey149 }150}151func (p *parser) pop() {152 p.stack = p.stack[:len(p.stack)-1]153 p.container = p.stack[len(p.stack)-1]154 p.resetState()155}156func (p *parser) resetState() {157 switch p.container.(type) {158 case nil:159 p.state = stateEnd160 case []interface{}:161 p.state = stateAfterArrayElement162 case map[string]interface{}:163 p.state = stateAfterObjectValue164 }165}166func invalidToken(t token) error {167 return &SyntaxError{fmt.Sprintf("json5: invalid input '%v'", t.input), t.line, t.column}168}169const (170 stateBeforeArrayElement = iota...`

resetState

Using AI Code Generation

`1import (2func main() {3 p := caddyfile.NewDispenser("file", strings.NewReader("example.com"))4 p.Next()5 p.Next()6 p.Next()7 fmt.Println(p.Next())8}9import (10func main() {11 p := caddyfile.NewDispenser("file", strings.NewReader("example.com"))12 p.Next()13 p.Next()14 p.Next()15 p.Next()16 fmt.Println(p.Next())17}`

resetState

Using AI Code Generation

`1import (2func main() {3 p := new(parser)4 p.resetState()5 fmt.Println(p)6}7import (8func main() {9 p := parser{}10 p.resetState()11 fmt.Println(p)12}13{0 false}14{0 false}`

resetState

Using AI Code Generation

`1import (2func main() {3 f, err := os.Open("test.txt")4 if err != nil {5 fmt.Println(err)6 }7 defer f.Close()8 scanner := bufio.NewScanner(f)9 scanner.Split(bufio.ScanLines)10 for scanner.Scan() {11 txtlines = append(txtlines, scanner.Text())12 }13 p := NewParser()14 p.Parse(txtlines)15 p.ResetState()16 p.Parse(txtlines)17}18import (19func main() {20 f, err := os.Open("test.txt")21 if err != nil {22 fmt.Println(err)23 }24 defer f.Close()25 scanner := bufio.NewScanner(f)26 scanner.Split(bufio.ScanLines)27 for scanner.Scan() {28 txtlines = append(txtlines, scanner.Text())29 }30 p := NewParser()31 p.Parse(txtlines)32}33import (34func main() {35 f, err := os.Open("test.txt")36 if err != nil {37 fmt.Println(err)38 }39 defer f.Close()`

## Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.