Best Gauge code snippet using parser.hasParseError
conceptParser_test.go
Source:conceptParser_test.go
...48 concepts, errs, err := AddConcepts([]string{path}, dictionary)49 c.Assert(err, IsNil)50 c.Assert(len(concepts), Equals, 2)51 c.Assert(len(errs) > 0, Equals, true)52 c.Assert(hasParseError("Duplicate concept definition found", path, 1, errs), Equals, true)53 c.Assert(hasParseError("Duplicate concept definition found", path, 4, errs), Equals, true)54}55func hasParseError(eMessage, fileName string, lineNo int, errs []ParseError) bool {56 for _, e := range errs {57 if e.Message == eMessage && e.FileName == fileName && e.LineNo == lineNo {58 return true59 }60 }61 return false62}63func (s *MySuite) TestDuplicateConceptsinMultipleFile(c *C) {64 dictionary := gauge.NewConceptDictionary()65 cpt1, _ := filepath.Abs(filepath.Join("testdata", "err", "cpt", "concept.cpt"))66 cpt2, _ := filepath.Abs(filepath.Join("testdata", "err", "cpt", "duplicate.cpt"))67 AddConcepts([]string{cpt1}, dictionary)68 concepts, errs, err := AddConcepts([]string{cpt2}, dictionary)69 c.Assert(err, IsNil)70 c.Assert(len(concepts), Equals, 2)71 c.Assert(len(errs), Equals, 4)72 c.Assert(hasParseError("Duplicate concept definition found", cpt1, 1, errs), Equals, true)73 c.Assert(hasParseError("Duplicate concept definition found", cpt1, 4, errs), Equals, true)74 c.Assert(hasParseError("Duplicate concept definition found", cpt2, 1, errs), Equals, true)75 c.Assert(hasParseError("Duplicate concept definition found", cpt2, 4, errs), Equals, true)76}77func (s *MySuite) TestCreateConceptDictionaryGivesAllParseErrors(c *C) {78 config.ProjectRoot, _ = filepath.Abs(filepath.Join("testdata", "err", "cpt"))79 _, res, err := CreateConceptsDictionary()80 c.Assert(err, IsNil)81 c.Assert(res.Ok, Equals, false)82 c.Assert(len(res.ParseErrors), Equals, 9)83}84func (s *MySuite) TestCreateConceptDictionary(c *C) {85 config.ProjectRoot, _ = filepath.Abs(filepath.Join("testdata", "dir1"))86 dict, res, err := CreateConceptsDictionary()87 c.Assert(err, IsNil)88 c.Assert(res.Ok, Equals, true)89 c.Assert(dict, NotNil)...
parse.go
Source:parse.go
1package css2import (3 "bytes"4 "fmt"5 "strconv"6 "github.com/tdewolff/parse/v2"7 "github.com/tdewolff/parse/v2/buffer"8)9var wsBytes = []byte(" ")10var endBytes = []byte("}")11var emptyBytes = []byte("")12// GrammarType determines the type of grammar.13type GrammarType uint3214// GrammarType values.15const (16 ErrorGrammar GrammarType = iota // extra token when errors occur17 CommentGrammar18 AtRuleGrammar19 BeginAtRuleGrammar20 EndAtRuleGrammar21 QualifiedRuleGrammar22 BeginRulesetGrammar23 EndRulesetGrammar24 DeclarationGrammar25 TokenGrammar26 CustomPropertyGrammar27)28// String returns the string representation of a GrammarType.29func (tt GrammarType) String() string {30 switch tt {31 case ErrorGrammar:32 return "Error"33 case CommentGrammar:34 return "Comment"35 case AtRuleGrammar:36 return "AtRule"37 case BeginAtRuleGrammar:38 return "BeginAtRule"39 case EndAtRuleGrammar:40 return "EndAtRule"41 case QualifiedRuleGrammar:42 return "QualifiedRule"43 case BeginRulesetGrammar:44 return "BeginRuleset"45 case EndRulesetGrammar:46 return "EndRuleset"47 case DeclarationGrammar:48 return "Declaration"49 case TokenGrammar:50 return "Token"51 case CustomPropertyGrammar:52 return "CustomProperty"53 }54 return "Invalid(" + strconv.Itoa(int(tt)) + ")"55}56////////////////////////////////////////////////////////////////57// State is the state function the parser currently is in.58type State func(*Parser) GrammarType59// Token is a single TokenType and its associated data.60type Token struct {61 TokenType62 Data []byte63}64func (t Token) String() string {65 return t.TokenType.String() + "('" + string(t.Data) + "')"66}67const debug = false68func dbg(format string, args ...interface{}) {69 if !debug {70 return71 }72 fmt.Printf("\033[32m"+format+"\033[0m\n", args...)73}74// Parser is the state for the parser.75type Parser struct {76 l *Lexer77 state []State78 err string79 errPos int80 buf []Token81 level int82 data []byte83 tt TokenType84 keepWS bool85 prevWS bool86 prevEnd bool87 prevComment bool88}89// NewParser returns a new CSS parser from an io.Reader. isInline specifies whether this is an inline style attribute.90func NewParser(r *parse.Input, isInline bool) *Parser {91 l := NewLexer(r)92 p := &Parser{93 l: l,94 state: make([]State, 0, 4),95 }96 if isInline {97 p.state = append(p.state, (*Parser).parseDeclarationList)98 } else {99 p.state = append(p.state, (*Parser).parseStylesheet)100 }101 return p102}103// HasParseError returns true if there is a parse error (and not a read error).104func (p *Parser) HasParseError() bool {105 return p.err != ""106}107// Err returns the error encountered during parsing, this is often io.EOF but also other errors can be returned.108func (p *Parser) Err() error {109 if p.err != "" {110 r := buffer.NewReader(p.l.r.Bytes())111 return parse.NewError(r, p.errPos, p.err)112 }113 return p.l.Err()114}115// Next returns the next Grammar. It returns ErrorGrammar when an error was encountered. Using Err() one can retrieve the error message.116func (p *Parser) Next() (GrammarType, TokenType, []byte) {117 p.err = ""118 if p.prevEnd {119 p.tt, p.data = RightBraceToken, endBytes120 p.prevEnd = false121 } else {122 p.tt, p.data = p.popToken(true)123 }124 gt := p.state[len(p.state)-1](p)125 return gt, p.tt, p.data126}127// Values returns a slice of Tokens for the last Grammar. Only AtRuleGrammar, BeginAtRuleGrammar, BeginRulesetGrammar and Declaration will return the at-rule components, ruleset selector and declaration values respectively.128func (p *Parser) Values() []Token {129 return p.buf130}131func (p *Parser) popToken(allowComment bool) (TokenType, []byte) {132 p.prevWS = false133 p.prevComment = false134 tt, data := p.l.Next()135 for !p.keepWS && tt == WhitespaceToken || tt == CommentToken {136 if tt == WhitespaceToken {137 p.prevWS = true138 } else {139 p.prevComment = true140 if allowComment && len(p.state) == 1 {141 break142 }143 }144 tt, data = p.l.Next()145 }146 return tt, data147}148func (p *Parser) initBuf() {149 p.buf = p.buf[:0]150}151func (p *Parser) pushBuf(tt TokenType, data []byte) {152 p.buf = append(p.buf, Token{tt, data})153}154////////////////////////////////////////////////////////////////155func (p *Parser) parseStylesheet() GrammarType {156 if p.tt == CDOToken || p.tt == CDCToken {157 return TokenGrammar158 } else if p.tt == AtKeywordToken {159 return p.parseAtRule()160 } else if p.tt == CommentToken {161 return CommentGrammar162 } else if p.tt == ErrorToken {163 return ErrorGrammar164 }165 return p.parseQualifiedRule()166}167func (p *Parser) parseDeclarationList() GrammarType {168 if p.tt == CommentToken {169 p.tt, p.data = p.popToken(false)170 }171 for p.tt == SemicolonToken {172 p.tt, p.data = p.popToken(false)173 }174 // IE hack: *color:red;175 if p.tt == DelimToken && p.data[0] == '*' {176 tt, data := p.popToken(false)177 p.tt = tt178 p.data = append(p.data, data...)179 }180 if p.tt == ErrorToken {181 return ErrorGrammar182 } else if p.tt == AtKeywordToken {183 return p.parseAtRule()184 } else if p.tt == IdentToken || p.tt == DelimToken {185 return p.parseDeclaration()186 } else if p.tt == CustomPropertyNameToken {187 return p.parseCustomProperty()188 }189 // parse error190 p.initBuf()191 p.l.r.Move(-len(p.data))192 p.err, p.errPos = fmt.Sprintf("CSS parse error: unexpected token '%s' in declaration", string(p.data)), p.l.r.Offset()193 p.l.r.Move(len(p.data))194 if p.tt == RightBraceToken {195 // right brace token will occur when we've had a decl error that ended in a right brace token196 // as these are not handled by decl error, we handle it here explicitly. Normally its used to end eg. the qual rule.197 p.pushBuf(p.tt, p.data)198 return ErrorGrammar199 }200 return p.parseDeclarationError(p.tt, p.data)201}202////////////////////////////////////////////////////////////////203func (p *Parser) parseAtRule() GrammarType {204 p.initBuf()205 parse.ToLower(p.data)206 atRuleName := p.data207 if len(atRuleName) > 0 && atRuleName[1] == '-' {208 if i := bytes.IndexByte(atRuleName[2:], '-'); i != -1 {209 atRuleName = atRuleName[i+2:] // skip vendor specific prefix210 }211 }212 atRule := ToHash(atRuleName[1:])213 first := true214 skipWS := false215 for {216 tt, data := p.popToken(false)217 if tt == LeftBraceToken && p.level == 0 {218 if atRule == Font_Face || atRule == Page {219 p.state = append(p.state, (*Parser).parseAtRuleDeclarationList)220 } else if atRule == Document || atRule == Keyframes || atRule == Media || atRule == Supports {221 p.state = append(p.state, (*Parser).parseAtRuleRuleList)222 } else {223 p.state = append(p.state, (*Parser).parseAtRuleUnknown)224 }225 return BeginAtRuleGrammar226 } else if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken {227 p.prevEnd = (tt == RightBraceToken)228 return AtRuleGrammar229 } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {230 p.level++231 } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {232 p.level--233 }234 if first {235 if tt == LeftParenthesisToken || tt == LeftBracketToken {236 p.prevWS = false237 }238 first = false239 }240 if len(data) == 1 && (data[0] == ',' || data[0] == ':') {241 skipWS = true242 } else if p.prevWS && !skipWS && tt != RightParenthesisToken {243 p.pushBuf(WhitespaceToken, wsBytes)244 } else {245 skipWS = false246 }247 if tt == LeftParenthesisToken {248 skipWS = true249 }250 p.pushBuf(tt, data)251 }252}253func (p *Parser) parseAtRuleRuleList() GrammarType {254 if p.tt == RightBraceToken || p.tt == ErrorToken {255 p.state = p.state[:len(p.state)-1]256 return EndAtRuleGrammar257 } else if p.tt == AtKeywordToken {258 return p.parseAtRule()259 } else {260 return p.parseQualifiedRule()261 }262}263func (p *Parser) parseAtRuleDeclarationList() GrammarType {264 for p.tt == SemicolonToken {265 p.tt, p.data = p.popToken(false)266 }267 if p.tt == RightBraceToken || p.tt == ErrorToken {268 p.state = p.state[:len(p.state)-1]269 return EndAtRuleGrammar270 }271 return p.parseDeclarationList()272}273func (p *Parser) parseAtRuleUnknown() GrammarType {274 p.keepWS = true275 if p.tt == RightBraceToken && p.level == 0 || p.tt == ErrorToken {276 p.state = p.state[:len(p.state)-1]277 p.keepWS = false278 return EndAtRuleGrammar279 }280 if p.tt == LeftParenthesisToken || p.tt == LeftBraceToken || p.tt == LeftBracketToken || p.tt == FunctionToken {281 p.level++282 } else if p.tt == RightParenthesisToken || p.tt == RightBraceToken || p.tt == RightBracketToken {283 p.level--284 }285 return TokenGrammar286}287func (p *Parser) parseQualifiedRule() GrammarType {288 dbg("parseQualifiedRule")289 p.initBuf()290 first := true291 inAttrSel := false292 skipWS := true293 var tt TokenType294 var data []byte295 for {296 if first {297 tt, data = p.tt, p.data298 p.tt = WhitespaceToken299 p.data = emptyBytes300 first = false301 } else {302 tt, data = p.popToken(false)303 }304 dbg("> tt=%s, data=%s", tt, string(data))305 if tt == LeftBraceToken && p.level == 0 {306 p.state = append(p.state, (*Parser).parseQualifiedRuleDeclarationList)307 return BeginRulesetGrammar308 } else if tt == ErrorToken {309 p.err, p.errPos = "CSS parse error: unexpected ending in qualified rule", p.l.r.Offset()310 return ErrorGrammar311 } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {312 p.level++313 } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {314 p.level--315 }316 if p.level == 0 && len(data) == 1 && (data[0] == ',' || data[0] == '>' || data[0] == '+' || data[0] == '~') {317 if data[0] == ',' {318 return QualifiedRuleGrammar319 }320 skipWS = true321 } else if p.prevWS && !skipWS && !inAttrSel {322 p.pushBuf(WhitespaceToken, wsBytes)323 } else {324 skipWS = false325 }326 if tt == LeftBracketToken {327 inAttrSel = true328 } else if tt == RightBracketToken {329 inAttrSel = false330 }331 p.pushBuf(tt, data)332 }333}334func (p *Parser) parseQualifiedRuleDeclarationList() GrammarType {335 for p.tt == SemicolonToken {336 p.tt, p.data = p.popToken(false)337 }338 if p.tt == RightBraceToken || p.tt == ErrorToken {339 p.state = p.state[:len(p.state)-1]340 return EndRulesetGrammar341 }342 return p.parseDeclarationList()343}344func (p *Parser) parseDeclaration() GrammarType {345 p.initBuf()346 parse.ToLower(p.data)347 ttName, dataName := p.tt, p.data348 tt, data := p.popToken(false)349 if tt != ColonToken {350 p.l.r.Move(-len(data))351 p.err, p.errPos = "CSS parse error: expected colon in declaration", p.l.r.Offset()352 p.l.r.Move(len(data))353 p.pushBuf(ttName, dataName)354 return p.parseDeclarationError(tt, data)355 }356 skipWS := true357 for {358 tt, data := p.popToken(false)359 if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken {360 p.prevEnd = (tt == RightBraceToken)361 return DeclarationGrammar362 } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {363 p.level++364 } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {365 p.level--366 }367 if len(data) == 1 && (data[0] == ',' || data[0] == '/' || data[0] == ':' || data[0] == '!' || data[0] == '=') {368 skipWS = true369 } else if (p.prevWS || p.prevComment) && !skipWS {370 p.pushBuf(WhitespaceToken, wsBytes)371 } else {372 skipWS = false373 }374 p.pushBuf(tt, data)375 }376}377func (p *Parser) parseDeclarationError(tt TokenType, data []byte) GrammarType {378 // we're on the offending (tt,data), keep popping tokens till we reach ;, }, or EOF379 p.tt, p.data = tt, data380 for {381 if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken {382 p.prevEnd = (tt == RightBraceToken)383 if tt == SemicolonToken {384 p.pushBuf(tt, data)385 }386 return ErrorGrammar387 } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {388 p.level++389 } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {390 p.level--391 }392 if p.prevWS {393 p.pushBuf(WhitespaceToken, wsBytes)394 }395 p.pushBuf(tt, data)396 tt, data = p.popToken(false)397 }398}399func (p *Parser) parseCustomProperty() GrammarType {400 p.initBuf()401 if tt, data := p.popToken(false); tt != ColonToken {402 p.l.r.Move(-len(data))403 p.err, p.errPos = "CSS parse error: expected colon in custom property", p.l.r.Offset()404 p.l.r.Move(len(data))405 return ErrorGrammar406 }407 val := []byte{}408 for {409 tt, data := p.l.Next()410 if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken {411 p.prevEnd = (tt == RightBraceToken)412 p.pushBuf(CustomPropertyValueToken, val)413 return CustomPropertyGrammar414 } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {415 p.level++416 } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {417 p.level--418 }419 val = append(val, data...)420 }421}...
hasParseError
Using AI Code Generation
1import (2func main() {3 f, err := parser.ParseFile(fset, "src.go", nil, parser.ImportsOnly)4 if err != nil {5 }6 if parser.HasParseError(f) {7 fmt.Println("f has parse error")8 } else {9 fmt.Println("f does not have parse error")10 }11}12S.No. Method & Description 1. func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (*ast.File, error) ParseFile parses the source code in the file identified by filename and returns the corresponding ast.File node. The source code may be provided as a string, []byte, or io.Reader, and the mode parameter controls the amount of source code parsed and other optional parser functionality. 2. func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (map[string]*ast.Package, error) ParseDir parses the package source files in the directory identified by path and returns the corresponding ast.Package nodes. The filter function determines which files to include in the package. The mode parameter controls the amount of source code parsed and other optional parser functionality. 3. func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode Mode) (ast.Expr, error) ParseExprFrom parses the source code and returns the corresponding ast.Expr node. The source code may be provided as a string, []byte, or io.Reader, and the mode parameter controls the amount of source code parsed and other optional parser functionality. 4. func ParseExpr(src string) (ast.Expr, error) ParseExpr is a wrapper around ParseExprFrom that parses a single expression from a string. 5. func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (*ast.File, error) ParseFile parses the source code in the file identified
hasParseError
Using AI Code Generation
1import (2func main() {3 fset := token.NewFileSet()4 src := []byte(`package main5import "fmt"6func main() {7 fmt.Println("Hello, 世界")8}`)9 f, err := parser.ParseFile(fset, "", src, 0)10 if err != nil {11 panic(err)12 }13 fmt.Println(f.Name.Name)14 fmt.Println(parser.HasParseError(f))15}16import (17func main() {18 fset := token.NewFileSet()
hasParseError
Using AI Code Generation
1import (2func main() {3 fset := token.NewFileSet()4 src := []byte("package main5func main() {6 println(1 + 27}")8 f, err := parser.ParseFile(fset, "", src, 0)9 if err != nil {10 }11 if f != nil {12 fmt.Println("no error")13 }14}151.go:4:6: expected ';', found 'println'16func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (*ast.File, error)
hasParseError
Using AI Code Generation
1import (2func main() {3 fs := token.NewFileSet()4 src := []byte("package main5func main() {6 println(x)7}")8 f, err := parser.ParseFile(fs, "src.go", src, 0)9 if err != nil {10 }11 fmt.Println(f)12 fmt.Println("Has parse error: ", parser.HasParseError(f))13}14Go | parser.ParseExprFrom() method15Go | parser.ParseExpr() method16Go | parser.ParseFile() method17Go | parser.ParseDir() method18Go | parser.ParseCommentMap() method19Go | parser.ParseComments() method20Go | parser.ParseExprFrom() method21Go | parser.ParseExpr() method22Go | parser.ParseFile() method23Go | parser.ParseDir() method24Go | parser.ParseCommentMap() method25Go | parser.ParseComments() method26Go | parser.ParseExprFrom() method27Go | parser.ParseExpr() method28Go | parser.ParseFile() method29Go | parser.ParseDir() method30Go | parser.ParseCommentMap() method31Go | parser.ParseComments() method32Go | parser.ParseExprFrom() method33Go | parser.ParseExpr() method34Go | parser.ParseFile() method35Go | parser.ParseDir() method36Go | parser.ParseCommentMap() method37Go | parser.ParseComments() method38Go | parser.ParseExprFrom() method39Go | parser.ParseExpr() method40Go | parser.ParseFile() method41Go | parser.ParseDir() method42Go | parser.ParseCommentMap() method43Go | parser.ParseComments() method44Go | parser.ParseExprFrom() method45Go | parser.ParseExpr() method46Go | parser.ParseFile() method47Go | parser.ParseDir() method48Go | parser.ParseCommentMap() method49Go | parser.ParseComments() method50Go | parser.ParseExprFrom() method51Go | parser.ParseExpr() method52Go | parser.ParseFile() method53Go | parser.ParseDir()
hasParseError
Using AI Code Generation
1import (2func main() {3 fset := token.NewFileSet()4 _, err := parser.ParseFile(fset, "test.go", "package main", 0)5 if err != nil {6 if _, ok := err.(parser.ErrorList); ok {7 fmt.Println("There was a parse error")8 }9 }10}
hasParseError
Using AI Code Generation
1import "fmt"2import "go/parser"3import "go/token"4import "io/ioutil"5func main() {6 input, err := ioutil.ReadFile("1.go")7 if err != nil {8 fmt.Println(err)9 }10 f, err := parser.ParseFile(fset, "1.go", input, 0)11 if err != nil {12 fmt.Println(err)13 }14 fmt.Println(f)15 fmt.Println(parser.HasParseError(f))16}17import "fmt"18import "go/parser"19import "go/token"20import "io/ioutil"21func main() {22 input, err := ioutil.ReadFile("1.go")23 if err != nil {24 fmt.Println(err)25 }26 f, err := parser.ParseFile(fset, "1.go", input, 0)27 if err != nil {28 fmt.Println(err)29 }30 fmt.Println(f)31 fmt.Println(parser.HasParseError(f))32}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!