scope.go 800 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package parser
  2. import (
  3. "github.com/robertkrimen/otto/ast"
  4. )
  5. type scope struct {
  6. outer *scope
  7. allowIn bool
  8. inIteration bool
  9. inSwitch bool
  10. inFunction bool
  11. declarationList []ast.Declaration
  12. labels []string
  13. }
  14. func (p *parser) openScope() {
  15. p.scope = &scope{
  16. outer: p.scope,
  17. allowIn: true,
  18. }
  19. }
  20. func (p *parser) closeScope() {
  21. p.scope = p.scope.outer
  22. }
  23. func (p *scope) declare(declaration ast.Declaration) {
  24. p.declarationList = append(p.declarationList, declaration)
  25. }
  26. func (p *scope) hasLabel(name string) bool {
  27. for _, label := range p.labels {
  28. if label == name {
  29. return true
  30. }
  31. }
  32. if p.outer != nil && !p.inFunction {
  33. // Crossing a function boundary to look for a label is verboten
  34. return p.outer.hasLabel(name)
  35. }
  36. return false
  37. }