scope.go 808 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package parser
  2. import (
  3. "github.com/robertkrimen/otto/ast"
  4. )
  5. type scope struct {
  6. outer *scope
  7. declarationList []ast.Declaration
  8. labels []string
  9. allowIn bool
  10. inIteration bool
  11. inSwitch bool
  12. inFunction bool
  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. }