scope.go 849 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 (self *_parser) openScope() {
  15. self.scope = &_scope{
  16. outer: self.scope,
  17. allowIn: true,
  18. }
  19. }
  20. func (self *_parser) closeScope() {
  21. self.scope = self.scope.outer
  22. }
  23. func (self *_scope) declare(declaration ast.Declaration) {
  24. self.declarationList = append(self.declarationList, declaration)
  25. }
  26. func (self *_scope) hasLabel(name string) bool {
  27. for _, label := range self.labels {
  28. if label == name {
  29. return true
  30. }
  31. }
  32. if self.outer != nil && !self.inFunction {
  33. // Crossing a function boundary to look for a label is verboten
  34. return self.outer.hasLabel(name)
  35. }
  36. return false
  37. }