Lines Matching refs:p
62 func parse(p *parser) (file *File, errs []error) {
66 errs = p.errors
73 defs := p.parseDefinitions()
74 p.accept(scanner.EOF)
75 errs = p.errors
76 comments := p.comments
79 Name: p.scanner.Filename,
87 p := newParser(r, scope)
88 p.eval = true
89 p.scanner.Filename = filename
91 return parse(p)
95 p := newParser(r, scope)
96 p.scanner.Filename = filename
98 return parse(p)
111 p := &parser{}
112 p.scope = scope
113 p.scanner.Init(r)
114 p.scanner.Error = func(sc *scanner.Scanner, msg string) {
115 p.errorf(msg)
117 p.scanner.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings |
119 p.next()
120 return p
123 func (p *parser) error(err error) {
124 pos := p.scanner.Position
126 pos = p.scanner.Pos()
132 p.errors = append(p.errors, err)
133 if len(p.errors) >= maxErrors {
138 func (p *parser) errorf(format string, args ...interface{}) {
139 p.error(fmt.Errorf(format, args...))
142 func (p *parser) accept(toks ...rune) bool {
144 if p.tok != tok {
145 p.errorf("expected %s, found %s", scanner.TokenString(tok),
146 scanner.TokenString(p.tok))
149 p.next()
154 func (p *parser) next() {
155 if p.tok != scanner.EOF {
156 p.tok = p.scanner.Scan()
157 if p.tok == scanner.Comment {
159 for p.tok == scanner.Comment {
160 lines := strings.Split(p.scanner.TokenText(), "\n")
161 if len(comments) > 0 && p.scanner.Position.Line > comments[len(comments)-1].End().Line+1 {
162 p.comments = append(p.comments, &CommentGroup{Comments: comments})
165 comments = append(comments, &Comment{lines, p.scanner.Position})
166 p.tok = p.scanner.Scan()
168 p.comments = append(p.comments, &CommentGroup{Comments: comments})
174 func (p *parser) parseDefinitions() (defs []Definition) {
176 switch p.tok {
178 ident := p.scanner.TokenText()
179 pos := p.scanner.Position
181 p.accept(scanner.Ident)
183 switch p.tok {
185 p.accept('+')
186 defs = append(defs, p.parseAssignment(ident, pos, "+="))
188 defs = append(defs, p.parseAssignment(ident, pos, "="))
190 defs = append(defs, p.parseModule(ident, pos))
192 p.errorf("expected \"=\" or \"+=\" or \"{\" or \"(\", found %s",
193 scanner.TokenString(p.tok))
198 p.errorf("expected assignment or module definition, found %s",
199 scanner.TokenString(p.tok))
205 func (p *parser) parseAssignment(name string, namePos scanner.Position,
210 pos := p.scanner.Position
211 if !p.accept('=') {
214 value := p.parseExpression()
223 if p.scope != nil {
225 if old, local := p.scope.Get(assignment.Name); old == nil {
226 p.errorf("modified non-existent variable %q with +=", assignment.Name)
228 p.errorf("modified non-local variable %q with +=", assignment.Name)
230 p.errorf("modified variable %q with += after referencing", assignment.Name)
232 val, err := p.evaluateOperator(old.Value, assignment.Value, '+', assignment.EqualsPos)
234 p.error(err)
240 err := p.scope.Add(assignment)
242 p.error(err)
250 func (p *parser) parseModule(typ string, typPos scanner.Position) *Module {
253 lbracePos := p.scanner.Position
254 if p.tok == '{' {
258 if !p.accept(p.tok) {
261 properties := p.parsePropertyList(true, compat)
262 rbracePos := p.scanner.Position
264 p.accept(')')
266 p.accept('}')
280 func (p *parser) parsePropertyList(isModule, compat bool) (properties []*Property) {
281 for p.tok == scanner.Ident {
282 property := p.parseProperty(isModule, compat)
285 if p.tok != ',' {
290 p.accept(',')
296 func (p *parser) parseProperty(isModule, compat bool) (property *Property) {
299 name := p.scanner.TokenText()
300 namePos := p.scanner.Position
301 p.accept(scanner.Ident)
302 pos := p.scanner.Position
306 if !p.accept(':') {
310 if !p.accept('=') {
315 if !p.accept(':') {
320 value := p.parseExpression()
330 func (p *parser) parseExpression() (value Expression) {
331 value = p.parseValue()
332 switch p.tok {
334 return p.parseOperator(value)
336 p.errorf("subtraction not supported: %s", p.scanner.String())
343 func (p *parser) evaluateOperator(value1, value2 Expression, operator rune,
348 if p.eval {
370 v.Properties, err = p.addMaps(v.Properties, e2.(*Map).Properties, pos)
390 func (p *parser) addMaps(map1, map2 []*Property, pos scanner.Position) ([]*Property, error) {
412 newProp.Value, err = p.evaluateOperator(prop1.Value, prop2.Value, '+', pos)
431 func (p *parser) parseOperator(value1 Expression) *Operator {
432 operator := p.tok
433 pos := p.scanner.Position
434 p.accept(operator)
436 value2 := p.parseExpression()
438 value, err := p.evaluateOperator(value1, value2, operator, pos)
440 p.error(err)
448 func (p *parser) parseValue() (value Expression) {
449 switch p.tok {
451 return p.parseVariable()
453 return p.parseIntValue()
455 return p.parseStringValue()
457 return p.parseListValue()
459 return p.parseMapValue()
461 p.errorf("expected bool, list, or string value; found %s",
462 scanner.TokenString(p.tok))
467 func (p *parser) parseVariable() Expression {
470 switch text := p.scanner.TokenText(); text {
473 LiteralPos: p.scanner.Position,
478 if p.eval {
479 if assignment, local := p.scope.Get(text); assignment == nil {
480 p.errorf("variable %q is not set", text)
490 NamePos: p.scanner.Position,
495 p.accept(scanner.Ident)
499 func (p *parser) parseStringValue() *String {
500 str, err := strconv.Unquote(p.scanner.TokenText())
502 p.errorf("couldn't parse string: %s", err)
507 LiteralPos: p.scanner.Position,
510 p.accept(scanner.String)
514 func (p *parser) parseIntValue() *Int64 {
516 literalPos := p.scanner.Position
517 if p.tok == '-' {
518 str += string(p.tok)
519 p.accept(p.tok)
520 if p.tok != scanner.Int {
521 p.errorf("expected int; found %s", scanner.TokenString(p.tok))
525 str += p.scanner.TokenText()
528 p.errorf("couldn't parse int: %s", err)
537 p.accept(scanner.Int)
541 func (p *parser) parseListValue() *List {
542 lBracePos := p.scanner.Position
543 if !p.accept('[') {
548 for p.tok != ']' {
549 element := p.parseExpression()
550 if p.eval && element.Type() != StringType {
551 p.errorf("Expected string in list, found %s", element.Type().String())
556 if p.tok != ',' {
561 p.accept(',')
564 rBracePos := p.scanner.Position
565 p.accept(']')
574 func (p *parser) parseMapValue() *Map {
575 lBracePos := p.scanner.Position
576 if !p.accept('{') {
580 properties := p.parsePropertyList(false, false)
582 rBracePos := p.scanner.Position
583 p.accept('}')