Пример #1
0
func (v *MiscCheck) Visit(s *SemanticAnalyzer, n parser.Node) {
	if _, ok := n.(*parser.FunctionDecl); ok {
		v.InFunction++
	}

	if v.InFunction <= 0 {
		switch n.(type) {
		case *parser.ReturnStat:
			s.Err(n, "%s must be in function", util.CapitalizeFirst(n.NodeName()))
		}
	} else {
		switch n.(type) {
		case *parser.TypeDecl:
			s.Err(n, "%s must not be in function", util.CapitalizeFirst(n.NodeName()))
		}
	}
}
Пример #2
0
func (v *MiscCheck) Visit(s *SemanticAnalyzer, n ast.Node) {
	switch n.(type) {
	case *ast.FunctionDecl, *ast.LambdaExpr:
		v.InFunction++
	}

	if v.InFunction <= 0 {
		switch n.(type) {
		case *ast.ReturnStat:
			s.Err(n, "%s must be in function", util.CapitalizeFirst(n.NodeName()))
		}
	} else {
		switch n.(type) {
		case *ast.TypeDecl:
			s.Err(n, "%s must not be in function", util.CapitalizeFirst(n.NodeName()))

		case *ast.FunctionDecl:
			if v.InFunction > 1 {
				s.Err(n, "%s must not be in function", util.CapitalizeFirst(n.NodeName()))
			}
		}
	}
}
Пример #3
0
func (v *BreakAndNextCheck) Visit(s *SemanticAnalyzer, n ast.Node) {
	switch n := n.(type) {
	case *ast.NextStat, *ast.BreakStat:
		if v.nestedLoopCount[v.functions[len(v.functions)-1]] == 0 {
			s.Err(n, "%s must be in a loop", util.CapitalizeFirst(n.NodeName()))
		}

	case *ast.LoopStat:
		v.nestedLoopCount[v.functions[len(v.functions)-1]]++

	case *ast.FunctionDecl:
		v.functions = append(v.functions, n.Function)
	case *ast.LambdaExpr:
		v.functions = append(v.functions, n.Function)
	}
}