Example #1
0
File: fs.go Project: hialin/hialin
func GetFileNex(fpath string) nex.Nex {
	abspath, err := filepath.Abs(fpath)
	if err != nil {
		return nex.NewErrorNex(err)
	}
	fpath = filepath.Clean(abspath)
	fi, err := os.Stat(fpath)
	if err != nil {
		return nex.NewErrorNex(err)
	}
	return fileNex{fpath, fi}
}
Example #2
0
File: fs.go Project: hialin/hialin
func (f binaryFileField) Get(loc int64) nex.Nex {
	var current [1]byte
	if _, err := f.file.Seek(loc, os.SEEK_SET); err != nil {
		return nex.NewErrorNex(err)
	}
	if n, err := f.file.Read(current[:]); err != nil {
		return nex.NewErrorNex(err)
	} else if n == 0 {
		return nex.NewErrorNex(io.EOF)
	}
	return byteNex(current[0])
}
Example #3
0
File: fs.go Project: hialin/hialin
func GetHomeDirNex() nex.Nex {
	usr, err := user.Current()
	if err != nil {
		return nex.NewErrorNex(err)
	}
	return GetFileNex(usr.HomeDir)
}
Example #4
0
func GetHostNex() nex.Nex {
	name, err := os.Hostname()
	if err != nil {
		return nex.NewErrorNex(err)
	}
	return hostNode{name}
}
Example #5
0
func Parse(r io.Reader) nex.Nex {
	if l == nil {
		s = syms.NewSymbolSet()
		l = lexer.NewLexer(&s)
		l.Ignore(syms.WS)

		functions = make(map[string]grammar.UserFn)
		functions[""] = nexBuilder
		functions["synth"] = nexBuilder
		functions["nil"] = nilHandler
		var err error
		g, err = grammar.NewGrammarBlock(l, readGrammar(), functions)
		if !tlog.Ok(err) {
			return nex.NewErrorNex(err)
		}

		p, err = parser.NewParserExt(g, false, false)
		if !tlog.Ok(err) {
			return nex.NewErrorNex(err)
		}
	}

	l.SetReader(r)
	pt, err := p.Parse(l)
	if err != nil {
		_, isParseError := err.(parser.UnexpectedTerminalParseError)
		if !isParseError {
			return nex.NewErrorNex(err)
		}
	}

	n, isNex := pt.(nex.Nex)
	if !isNex {
		n = makeNexFromArray(g, g.Rules.Get(0), pt)
	}

	return n
}
Example #6
0
// Parse function returns a parse nex tree of the file
func Parse(filePath string) nex.Nex {
	if strings.HasSuffix(filePath, ".go") {
		f, err := os.Open(filePath)
		if !tlog.Ok(err) {
			return nex.NewErrorNex(err)
		}
		defer f.Close()
		reader := bufio.NewReader(f)
		return golang.Parse(reader)
	}
	// if strings.HasSuffix(filePath, ".json") {
	// 	return json.Parse(filePath)
	// }
	return nex.NewErrorNexFromString("unknown file, cannot parse")
}