func skipWS(reader *bufio.Reader) (eof bool) { eof = false for { rune, size, _ := reader.ReadRune() if size == 0 { eof = true break } if rune == 32 { continue } if rune == 13 { continue } if rune == 10 { continue } if rune == 9 { continue } reader.UnreadRune() break } return eof }
// Parse a address greedily from the buffer, if the first non-witespace // token found is not a valid start of an adr nil, nil is returned // If a valid adr is parsed, !nil, nil is returned // If there is an error whilst parsing the addr nil, !nil is returned // Note that it will consume all spaces in the buffer to check the next char func Parse(b *bufio.Reader) (*Node, error) { // TODO: Change from bytes.Bufer to something nicer... Best is an interface.. //Check for start, is it the start of an adr tl := newLexer(b) tl.consume(unicode.IsSpace) r, _, e := b.ReadRune() if e != nil { return nil, nil } b.UnreadRune() if !(unicode.IsDigit(r) || r == '/' || r == '+' || r == '-' || r == '.' || r == '$' || r == '\'' || r == '#' || r == ',' || r == ';') { return nil, nil } fullNode = nil l := newLexer(b) ok := yyParse(l) if ok != 0 { // There was an error whils parsing: return nil, err } return fullNode, nil }
func preprocess(buf []byte, r *bufio.Reader) (int, error) { i := 0 for c, n, err := r.ReadRune(); err == nil; c, n, err = r.ReadRune() { if i+n > len(buf) { // We don't have room so unread the rune and return. r.UnreadRune() return i, err } switch c { case '\x00': if len(buf)-1 < i+len(unknownRune) { copy(buf[i:len(unknownRune)], unknownRune) } else { // We don't have room so unread the rune and // return. r.UnreadRune() return i + n, err } case '\r': buf[i] = '\n' nxt, err := r.Peek(1) if err == nil && len(nxt) == 1 && nxt[0] == '\n' { r.ReadByte() } case '\f': buf[i] = '\n' default: copy(buf[i:i+n], []byte(string(c))) } i += n } return i, nil }
func nextLine(f *bufio.Reader) { // Consume whitespace for { r, _, err := f.ReadRune() if err != nil { if err == io.EOF { return } panic(err.Error()) } if r != ' ' && r != '\t' { f.UnreadRune() break } } // Consume any number of endline runes for { r, _, err := f.ReadRune() if err != nil { if err == io.EOF { return } panic(err.Error()) } if r != '\r' && r != '\n' { f.UnreadRune() return } } }
// translate Esc[X func escapeExKey(r rune, reader *bufio.Reader) rune { switch r { case 'D': r = CharBackward case 'C': r = CharForward case 'A': r = CharPrev case 'B': r = CharNext case 'H': r = CharLineStart case 'F': r = CharLineEnd default: if r == '3' && reader != nil { d, _, _ := reader.ReadRune() if d == '~' { r = CharDelete } else { reader.UnreadRune() } } } return r }
func (p *parser) nextToken(reader *bufio.Reader) (string, syms.SymbolID) { var sid stateID var str bytes.Buffer s := p.all[sid] for { r, _, err := reader.ReadRune() if err != nil { if sid == 0 { if err == io.EOF { return "", syms.EOF } return "", syms.ERROR } return str.String(), s.id } sid = s.tr[r] if sid == 0 { // fmt.Println("nextToken: unaccepted ", string(r), " returning ", s.id) reader.UnreadRune() return str.String(), s.id } // fmt.Println("nextToken: move via ", string(r), " to ", sid) s = p.all[sid] str.WriteRune(r) } }
// translate EscX to Meta+X func escapeKey(r rune, reader *bufio.Reader) rune { switch r { case 'b': r = MetaBackward case 'f': r = MetaForward case 'd': r = MetaDelete case CharTranspose: r = MetaTranspose case CharBackspace: r = MetaBackspace case 'O': d, _, _ := reader.ReadRune() switch d { case 'H': r = CharLineStart case 'F': r = CharLineEnd default: reader.UnreadRune() } case CharEsc: } return r }
func (sr *StandardReader) NextIdentifier(in *bufio.Reader) string { var id []rune if sr.cIdentifier != "" { rv := sr.cIdentifier sr.cIdentifier = "" return rv } for { c, _, err := in.ReadRune() if err != nil { if len(id) != 0 { break } sr.Error(errors.New(fmt.Sprintf("expected identifier (%s)", err.Error()))) } var ok bool if len(id) == 0 { ok = sr.IdentifierStart(c) } else { ok = sr.IdentifierPart(c) } if !ok { in.UnreadRune() if len(id) == 0 { break } sr.Error(errors.New(fmt.Sprintf("expected identifier (%s)", err.Error()))) } sr.pos += 1 sr.col += 1 id = append(id, c) } return string(id) }
func readSexp(r *bufio.Reader) (*Sexp, error) { // read ( or atom c, _, err := r.ReadRune() if err != nil { return nil, err } else if c != '(' { r.UnreadRune() return readAtom(r) } // read two space-separated Sexps x, err := readSexp(r) if err != nil { return nil, err } if err = expect(r, ' '); err != nil { return nil, err } y, err := readSexp(r) if err != nil { return nil, err } // read ) if err = expect(r, ')'); err != nil { return nil, err } return &Sexp{X: x, Y: y}, nil }
func parse(r *bufio.Reader, line, col *int, inList bool) (n Node, err error) { err = readSeparator(r, List, line, col) if err != nil { return } c, _, err := r.ReadRune() checkNewline(c, line, col) switch c { case comment: err = readComment(r, line, col) case listStart: n, err = readList(r, line, col) case listEnd: r.UnreadRune() case groupStart: n, err = readGroup(r, line, col) case groupEnd: r.UnreadRune() default: n, err = readWord(r, line, col) } return }
func (sr *StandardReader) TestPeek(in *bufio.Reader, c rune) bool { c0, _, err := in.ReadRune() if err != nil { return false } in.UnreadRune() return c == c0 }
func checkEmpty(data *bufio.Reader) (err error) { if _, _, err = data.ReadRune(); err != nil { return errors.New("unable to locate front matter") } if err = data.UnreadRune(); err != nil { return errors.New("unable to unread first charactor in page buffer.") } return }
// PeeKNext peek next valid (non space) rune from an input buffer. func peekNext(in *bufio.Reader) (rune, error) { if err := skipSpaces(in); err != nil { return rune(0), err } r, _, err := in.ReadRune() if err != nil { return rune(0), err } in.UnreadRune() return r, nil }
// SkipSpaces skip spaces from an input buffer. func skipSpaces(in *bufio.Reader) error { for { r, _, err := in.ReadRune() if err != nil { return err } if !unicode.IsSpace(r) { in.UnreadRune() return nil } } }
func consumeSpaces(in *bufio.Reader) (n int64, err error) { r := ' ' var c int for unicode.IsSpace(r) { n += int64(c) r, c, err = in.ReadRune() if err != nil { return } } err = in.UnreadRune() return }
func readName(r *bufio.Reader) string { v := []rune{} for { c := Rune(r) if ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('_' == c) { v = append(v, c) } else { r.UnreadRune() break } } return string(v) }
func doRuneRead(r *bufio.Reader) { ru, size, err := r.ReadRune() if err != nil { log.Fatalf("failed reading rune: %s", err) } log.Printf("Got rune %U of size %d (it looks like %q in Go)", ru, size, ru) log.Printf("Didn't mean to read that though, putting it back") err = r.UnreadRune() if err != nil { log.Fatalf("failed unreading a rune: %s", err) } }
func readRune(r *bufio.Reader) (rune, error) { r1, _, err := r.ReadRune() if r1 == '\r' { r1, _, err = r.ReadRune() if err == nil { if r1 != '\n' { r.UnreadRune() r1 = '\r' } } } return r1, err }
func SkipWhite(r *bufio.Reader) { for { w, _, err := r.ReadRune() if err != nil { panic(err) } if unicode.IsSpace(w) { continue } r.UnreadRune() return } }
func readSeparator(r *bufio.Reader, nt NodeType, line, col *int) (err error) { c, _, err := r.ReadRune() checkNewline(c, line, col) for err == nil && isSeparator(c, nt) { c, _, err = r.ReadRune() checkNewline(c, line, col) } if err == nil || err == io.EOF { r.UnreadRune() } return err }
func isNegativeNumber(pc, c rune, r *bufio.Reader) bool { if c != '-' { return false } if pc != 0 && !isSeparator(pc, Word) { return false } nc, _, _ := r.ReadRune() r.UnreadRune() return unicode.IsDigit(nc) }
func chompWhitespace(data *bufio.Reader) (r rune, err error) { for { r, _, err = data.ReadRune() if err != nil { return } if unicode.IsSpace(r) { continue } if err := data.UnreadRune(); err != nil { return r, errors.New("unable to unread first charactor in front matter.") } return r, nil } }
func ReadName(r *bufio.Reader) *Name { v := []rune{} for { c, _, err := r.ReadRune() if err != nil { panic(err) } if nameRuneP(c) { v = append(v, c) } else { r.UnreadRune() return &Name{string(v)} } } }
func skipTo(reader *bufio.Reader, stopRune rune) (eof bool) { eof = false for { rune, size, _ := reader.ReadRune() if size == 0 { eof = true break } if rune == stopRune { reader.UnreadRune() break } } return eof }
func readAtom(r *bufio.Reader) (*Sexp, error) { // read until non-atom rune encountered atom := "" for { c, _, err := r.ReadRune() if err != nil { return nil, err } else if c == '(' || c == ')' || c == ' ' { r.UnreadRune() break } atom += string(c) } return newAtom(atom), nil }
func skipComment(r *bufio.Reader) (bool, error) { bom := "\xEF\xBB\xBF" if ba, err := r.Peek(len(bom)); err != nil { return false, err } else if string(ba) == bom { _, _ = r.Read(ba) } if c, _, err := r.ReadRune(); err != nil { return false, err } else if c == '#' { _, err = r.ReadBytes('\n') return true, err } return false, r.UnreadRune() }
func nextString(f *bufio.Reader) string { result := make([]rune, 0, 10) for { r, _, err := f.ReadRune() if err != nil { panic(err.Error()) } if r == '\t' { return string(result) } if r == '\r' || r == '\n' { f.UnreadRune() return string(result) } result = append(result, r) } }
func readTrailer(r *bufio.Reader) []bool { v := []bool{} for { c, _, err := r.ReadRune() if err != nil { panic(err) } switch c { case 'a': v = append(v, true) case 'd': v = append(v, false) default: r.UnreadRune() return v } } }
func readNumber(r *bufio.Reader) float64 { v := "" for { c := Rune(r) switch c { case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'x', 'e', '-', '+': v += string(c) default: r.UnreadRune() f, err := strconv.ParseFloat(v, 64) if err != nil { panic(fmt.Sprintf("ParseFloat Error: %v", v)) } return f } } return 0.0 }
func readUntil(r *bufio.Reader, line, col *int, untilChar rune) (n Node, err error) { var fn Node = nil var pn Node = nil var nn Node = nil var closed bool = false for { nn, err = parse(r, line, col, true) if err != nil { break } if nn != nil { if pn != nil { pn.addNode(nn) } pn = nn if fn == nil { fn = nn } } err = readSeparator(r, List, line, col) if err != nil { break } c, _, err := r.ReadRune() if err != nil { break } if c == untilChar { closed = true break } r.UnreadRune() } if err == io.EOF && !closed { err = io.ErrUnexpectedEOF } return fn, err }