// 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 Rune(r *bufio.Reader) rune { v, _, err := r.ReadRune() if err != nil { panic(err) } return v }
func readRune(r *bufio.Reader) (rune, error) { rn, _, err := r.ReadRune() if err == io.EOF { return EOF, nil } return rn, err }
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 (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 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 }
// CharCount scans a *bufio.Reader and returns a map of the counts of its // Unicode character types. func CharCount(in *bufio.Reader) map[string]int { counts := make(map[string]int) // counts of Unicode character types for { r, n, err := in.ReadRune() // returns rune, nbytes, error if err == io.EOF { break } if err != nil { fmt.Fprintf(os.Stderr, "charcount: %v\n", err) os.Exit(1) } switch { case r == unicode.ReplacementChar && n == 1: counts["invalid"]++ case unicode.IsControl(r): counts["control"]++ case unicode.IsLetter(r): counts["letter"]++ case unicode.IsMark(r): counts["mark"]++ case unicode.IsNumber(r): counts["number"]++ case unicode.IsPunct(r): counts["punct"]++ case unicode.IsSpace(r): counts["space"]++ case unicode.IsSymbol(r): counts["symbol"]++ } } return counts }
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 } } }
func main() { scripts := flag.Bool("scripts", false, "print contained script names") cats := flag.Bool("cats", false, "print unicode categories") nochars := flag.Bool("no-chars", false, "do not print character information") flag.Parse() var reader *bufio.Reader args := flag.Args() if len(args) > 0 { is, err := os.Open(args[0]) if err != nil { panic(err) } defer is.Close() reader = bufio.NewReader(is) } else { reader = bufio.NewReader(os.Stdin) } chars := make(map[rune]int) for r, _, err := reader.ReadRune(); err == nil; r, _, err = reader.ReadRune() { chars[r]++ } if !*nochars { printChars(chars) } if *cats { printCategories(chars) } if *scripts { printScripts(chars) } }
// Lint reads the provided reader (with an optional associated path) // and checks the markdown for basic errors. Any errors found are // sent to the provided out channel func Lint(reader *bufio.Reader, path string, out chan<- error) { brackets, parens := list.New(), list.New() line := 1 column := 1 enDashes := make(map[int]int) pos := int64(0) // Parse the file for { r, size, err := reader.ReadRune() pos += int64(size) if err != nil { if err != io.EOF { out <- fmt.Errorf("Error reading from %s - %s", path, err) } break } switch r { case '[': brackets.PushFront(syntaxError{line, column, pos}) case ']': top := brackets.Front() if top == nil { basicError := fmt.Errorf(`Bad Markdown URL in %s: extra closing bracket at line %d, column %d`, path, line, column) out <- usefulError(path, pos, basicError) } else { brackets.Remove(top) } case '(': parens.PushFront(syntaxError{line, column, pos}) case ')': top := parens.Front() if top == nil { basicError := fmt.Errorf(`Bad Markdown URL in %s: extra closing parenthesis at line %d, column %d`, path, line, column) out <- usefulError(path, pos, basicError) } else { parens.Remove(top) } case '–': enDashes[line]++ case '\n': line++ column = 0 } column++ } // Check the results and accumulate any problems checkHanging(brackets, "bracket", out, path) checkHanging(parens, "parenthesis", out, path) for line := range enDashes { out <- fmt.Errorf("literal en dash at %s:%d - please use -- instead", path, line) } }
func main() { flag.Usage = usage flag.Parse() var r *bufio.Reader if flag.NArg() < 1 { r = bufio.NewReader(os.Stdin) } else { line := strings.Join(flag.Args(), " ") r = bufio.NewReader(strings.NewReader(line)) } for { ch, _, err := r.ReadRune() if err != nil { break } switch { case 'A' <= ch && ch <= 'M', 'a' <= ch && ch <= 'm': ch += 13 case 'N' <= ch && ch <= 'Z', 'n' <= ch && ch <= 'z': ch -= 13 } fmt.Printf("%c", ch) } }
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 (_ myHandler) Handle(reader *bufio.Reader, t *term.Full, c <-chan WindowSize) error { go func() { for ws := range c { t.SetDimensions(ws.Width, ws.Height) } }() t.Clear() for { r, _, err := reader.ReadRune() Panic(err) fmt.Printf("'%c'\n", r) t.Clear() t.Pos(10, 5) t.Printf("'%c' ", r) t.Attr().Fore(term.Red).Done() t.Printf("red ") t.Attr().Fore(term.Default).Done() t.Printf("default ") t.Attr().Fore(term.Red).Bright().Done() t.Printf("brightred ") t.Attr().Reset().Done() t.Printf("reset ") if t.Error() != nil { fmt.Printf("%v\n", t.Error()) } } return nil }
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 readQuoted(reader *bufio.Reader, delim rune) (result string, eof bool) { var b bytes.Buffer esc := false for { r, s, _ := reader.ReadRune() if s == 0 { eof = true break } if esc { switch r { case 'n': b.WriteRune(10) case 't': b.WriteRune(8) default: b.WriteRune(r) } esc = false } else { switch r { case delim: return b.String(), eof case '\\': esc = true default: b.WriteRune(r) } } } return b.String(), eof }
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 getc(b *bufio.Reader) rune { c, _, err := b.ReadRune() if err != nil { return eof } return c }
func count(in *bufio.Reader) (nl, nw, nr, nc int, err error) { inword := false for { var r rune var sz int r, sz, err = in.ReadRune() if err == io.EOF { err = nil break } if err != nil { return } nr++ nc += sz if r == '\n' { nl++ } if unicode.IsSpace(r) && inword { inword = false nw++ } else if !unicode.IsSpace(r) { inword = true } } return }
// Reads a character and returns its index in the vocabulary func ReadCharIndex(fin *bufio.Reader) (int, error) { var char rune char, _, err := fin.ReadRune() if err == io.EOF { return -1, err } return SearchVocab(char), nil }
func readRune(state *LexerState, reader *bufio.Reader) (rune int, size int, err os.Error) { rune, size, err = reader.ReadRune() if size > 0 { state.rune += 1 } 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 expect(r *bufio.Reader, exp rune) error { c, _, err := r.ReadRune() if err != nil { return err } else if c != exp { return errors.New("expected " + string(exp) + ", got " + string(c)) } return nil }
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 }
func readComment(r *bufio.Reader, line, col *int) (err error) { c, _, err := r.ReadRune() checkNewline(c, line, col) for err == nil && c != newLine { c, _, err = r.ReadRune() checkNewline(c, line, col) } return err }
func lint(reader *bufio.Reader, path string, out chan<- error) lintResult { res := lintResult{list.New(), list.New(), make(map[int]int), make(map[int]int)} line := 1 column := 1 pos := int64(0) // Parse the file for { r, size, err := reader.ReadRune() pos += int64(size) if err != nil { if err != io.EOF { out <- fmt.Errorf("Error reading from %s - %s", path, err) } break } switch r { case '[': res.brackets.PushFront(syntaxError{line, column, pos}) case ']': top := res.brackets.Front() if top == nil { basicError := fmt.Errorf(`Bad Markdown URL in %s: extra closing bracket at line %d, column %d`, path, line, column) out <- usefulError(path, pos, basicError) } else { res.brackets.Remove(top) } case '(': res.parens.PushFront(syntaxError{line, column, pos}) case ')': top := res.parens.Front() if top == nil { basicError := fmt.Errorf(`Bad Markdown URL in %s: extra closing parenthesis at line %d, column %d`, path, line, column) out <- usefulError(path, pos, basicError) } else { res.parens.Remove(top) } case '–': res.enDashes[line]++ case '—': res.emDashes[line]++ case '\n': line++ column = 0 } column++ } return res }
func fgetln(r *bufio.Reader) (string, error) { b := new(bytes.Buffer) for { c, _, err := r.ReadRune() if err != nil || c == '\n' { return b.String(), err } b.WriteRune(c) } }
// 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 }
func skipline(r *bufio.Reader) (err error) { var char rune for { char, _, err = r.ReadRune() if err != nil { return err } if char == '\n' { return nil } } }