Пример #1
0
func (p *parser) init(file *token.File, fname, src string) {
	p.file = file
	p.scanner.Init(p.file, src)
	p.listok = false
	p.curScope = ast.NewScope(nil)
	p.topScope = p.curScope
	p.next()
}
Пример #2
0
func (p *parser) init(f *token.File, name string, src io.Reader, s *ast.Scope) {
	if s == nil {
		s = ast.NewScope(nil)
	}
	p.file = f
	p.scanner.Init(p.file, src)
	p.curScope = s
	p.topScope = s
	p.next()
}
Пример #3
0
// ParseFile parses the file identified by filename and returns a pointer
// to an ast.File object. The file should contain Calc source code and
// have the .calc file extension.
// The returned AST object ast.File is nil if there is an error.
func ParseFile(fset *token.FileSet, filename, src string) (*ast.File, error) {
	var r io.Reader
	var sz int64
	if src == "" {
		f, err := os.Open(filename)
		if err != nil {
			return nil, err
		}
		defer f.Close()

		fi, err := f.Stat()
		if err != nil {
			return nil, err
		}

		if ext := filepath.Ext(fi.Name()); ext != ".calc" {
			return nil, fmt.Errorf("invalid file extension %s, must be .calc", ext)
		}
		r = f
		sz = fi.Size()
		fmt.Printf("%s:%d\n", fi.Name(), sz)
	} else {
		sr := strings.NewReader(src)
		r = io.Reader(sr)
		sz = sr.Size()
	}

	file := fset.Add(filepath.Base(filename), int(sz))
	var p parser
	p.init(file, filename, r, ast.NewScope(nil))
	f := p.parseFile()

	if p.errors.Count() > 0 {
		return nil, p.errors
	}

	return f, nil
}
Пример #4
0
func (p *parser) openScope() {
	p.curScope = ast.NewScope(p.curScope)
}