Skip to content

Commit

Permalink
Have parser keep track of file name and line number.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmacinnis committed Nov 29, 2015
1 parent 7c5daf5 commit fb4260b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 44 deletions.
2 changes: 1 addition & 1 deletion bin/test.oh
Expand Up @@ -18,6 +18,6 @@ while (define $path: readline) {
sed -e "s/^#[+-] //g" |
prefix-lines $file

$path | prefix-lines $file
dynamic $stderr = $stdout; $path | prefix-lines $file
} | sort | uniq -u

3 changes: 0 additions & 3 deletions lib/boot.oh
Expand Up @@ -189,9 +189,6 @@ define source: syntax e (name) as {

define f: open r- name

set-line-number: integer 0
set-source-file name

define r: cons () ()
define c = r
while (define l: f::read) {
Expand Down
3 changes: 0 additions & 3 deletions pkg/boot/generated.go
Expand Up @@ -196,9 +196,6 @@ define source: syntax e (name) as {
define f: open r- name
set-line-number: integer 0
set-source-file name
define r: cons () ()
define c = r
while (define l: f::read) {
Expand Down
17 changes: 10 additions & 7 deletions pkg/parser/parser.go
Expand Up @@ -17,7 +17,8 @@ type scanner struct {
task *task.Task

cursor int
indent int
lineno int
filename string
previous rune
start int
state int
Expand Down Expand Up @@ -93,9 +94,9 @@ main:
break
}

if s.task != nil {
s.task.Line++
}
s.lineno++
s.task.File = s.filename
s.task.Line = s.lineno

runes := []rune(line)
last := len(runes) - 2
Expand Down Expand Up @@ -285,19 +286,21 @@ main:
}

func (s *scanner) Error(msg string) {
s.task.PrintError(msg)
task.PrintError(s.filename, s.lineno, msg)
}

func Parse(t *task.Task,
r common.ReadStringer,
f string,
d func(string, uintptr) Cell,
p func(Cell)) {
c func(Cell)) {

s := new(scanner)

s.deref = d
s.filename = f
s.input = r
s.process = p
s.process = c
s.task = t

restart:
Expand Down
13 changes: 8 additions & 5 deletions pkg/task/task.go
Expand Up @@ -25,7 +25,7 @@ type ui interface {
ReadString(delim byte) (line string, err error)
}

type reader func(*Task, common.ReadStringer,
type reader func(*Task, common.ReadStringer, string,
func(string, uintptr) Cell, func(Cell))

const (
Expand Down Expand Up @@ -788,6 +788,11 @@ func Pgid() int {
return pgid
}

func PrintError(file string, line int, msg string) {
file = path.Base(file)
fmt.Fprintf(os.Stderr, "%s: %d: %v\n", file, line, msg)
}

func Resolve(s Context, e *Env, k *Symbol) (v Reference) {
v = nil

Expand All @@ -811,9 +816,8 @@ func Start(parser reader, cli ui) {
<-task0.Done
}

task0.File = "boot.oh"
b := bufio.NewReader(strings.NewReader(boot.Script))
parse(task0, b, deref, eval)
parse(task0, b, "boot.oh", deref, eval)

/* Command-line arguments */
args := Null
Expand Down Expand Up @@ -856,8 +860,7 @@ func Start(parser reader, cli ui) {

pgid = BecomeProcessGroupLeader()

task0.File = "oh"
parse(task0, cli, deref, evaluate)
parse(task0, cli, "oh", deref, evaluate)

fmt.Printf("\n")
cli.Close()
Expand Down
32 changes: 7 additions & 25 deletions pkg/task/types.go
Expand Up @@ -10,7 +10,6 @@ import (
. "github.com/michaelmacinnis/oh/pkg/cell"
"math/big"
"os"
"path"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -647,7 +646,11 @@ func IsPipe(c Cell) bool {
func NewPipe(l Context, r *os.File, w *os.File) Context {
p := &Pipe{
Scope: NewScope(l.Expose(), conduitEnv()),
b: nil, c: nil, d: nil, r: r, w: w,
b: nil,
c: nil,
d: nil,
r: r,
w: w,
}

if r == nil && w == nil {
Expand Down Expand Up @@ -709,7 +712,7 @@ func (p *Pipe) Read(t *Task) Cell {
p.c = make(chan Cell)
p.d = make(chan bool)
go func() {
parse(t, p.reader(), deref, func(c Cell) {
parse(t, p.reader(), p.r.Name(), deref, func(c Cell) {
p.c <- c
<-p.d
})
Expand Down Expand Up @@ -1475,27 +1478,6 @@ func (t *Task) Lookup(sym *Symbol, simple bool) (bool, string) {
return true, ""
}

func (t *Task) PrintError(msg string) {
file := "oh"
line := 0

if t != nil {
file = path.Base(t.File)
line = t.Line

t.File = "oh"
t.Line = 0
}

if line != 0 {
msg = fmt.Sprintf("%s: %d: %v\n", file, line, msg)
} else {
msg = fmt.Sprintf("%s: %v\n", file, msg)
}

fmt.Print(msg)
}

func (t *Task) Run(end Cell) (successful bool) {
successful = true

Expand All @@ -1505,7 +1487,7 @@ func (t *Task) Run(end Cell) (successful bool) {
return
}

t.PrintError(fmt.Sprintf("%v", r))
PrintError(t.File, t.Line, fmt.Sprintf("%v", r))

successful = false
}()
Expand Down

0 comments on commit fb4260b

Please sign in to comment.