package iskra import ( "bytes" ) // Token is one token of code with its class. type Token struct { Text string Class uint8 } // Tokenize splits one content string into two-char token chunks by class: // // [0] 0 = punctuation, 1 = identifier/keyword // [1] ' ' for anything but a call, '(' for an identifier followed by '(' // // This is extract.mx TokenizeToken, which the content token pool format is // defined by: MetaFile.TokenPool records TokenizeToken id runs and the reader // renders a chunk of two chars back with " ". Joining Decode with " " gives a // form whose dictionary ids are kept in MetaRecord.Extra[0] for a single-chunk // token. It is not the language tokenizer (tokenizeEN, tokenizeJA), which is // part of the extraction port. func Tokenize(content []byte) []Token { var out []Token for i := 0; i < len(content); i += 2 { end := i + 2 end = min(end, len(content)) chunk := content[i:end] isIdent := chunk[0] == '_' || chunk[0] >= 'a' && chunk[0] <= 'z' || chunk[0] >= 'A' && chunk[0] <= 'Z' class := TokModifier if isIdent { class = TokNoun if len(chunk) > 1 && chunk[1] == '(' { class = TokVerb } } out = append(out, Token{Text: string(chunk), Class: class}) } return out } // TokenizeWords splits text into whole-word tokens for a word dictionary: // identifier runs, decimal and hex numbers, quoted and raw strings, a run of // same-class punctuation, and each whitespace run as one token, so that joining // the tokens reproduces the input exactly. func TokenizeWords(content []byte) []Token { var out []Token i, n := 0, len(content) for i < n { c := content[i] switch { case c == '\n' || c == '\r': out = append(out, Token{Text: string(c), Class: TokModifier}) i++ case c == ' ' || c == '\t': j := i for j < n && (content[j] == ' ' || content[j] == '\t') { j++ } out = append(out, Token{Text: string(content[i:j]), Class: TokModifier}) i = j case isIdentStart(c) || c >= 0x80: j := i for j < n && (isIdentStart(content[j]) || (content[j] >= '0' && content[j] <= '9') || content[j] >= 0x80) { j++ } class := TokNoun if j < n && content[j] == '(' { class = TokVerb } out = append(out, Token{Text: string(content[i:j]), Class: class}) i = j case c >= '0' && c <= '9': j := i for j < n && (isIdentStart(content[j]) || (content[j] >= '0' && content[j] <= '9') || content[j] == '.') { j++ } out = append(out, Token{Text: string(content[i:j]), Class: TokLiteral}) i = j case c == '"' || c == '`' || c == '\'': j := i + 1 for j < n { if content[j] == '\\' && j+1 < n { j += 2 continue } if content[j] == c { j++ break } j++ } if j > n { j = n } out = append(out, Token{Text: string(content[i:j]), Class: TokLiteral}) i = j default: j := i + 1 for j < n && punctClass(content[j]) == punctClass(c) && content[j] != '"' && content[j] != '`' && content[j] != '\'' { j++ } out = append(out, Token{Text: string(content[i:j]), Class: TokModifier}) i = j } } return out } func isIdentStart(c byte) bool { return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') } // punctClass groups operator bytes so runs of them tokenise as one token. func punctClass(c byte) int { switch c { case '=', '+', '-', '*', '/', '<', '>', '!', '&', '|', '^', '%': return 1 case ':': return 2 case '.', ',': return 3 default: return 4 } } // SplitLines splits on '\n', dropping a trailing '\r'. func SplitLines(data []byte) [][]byte { var lines [][]byte for len(data) > 0 { idx := bytes.IndexByte(data, '\n') if idx < 0 { lines = append(lines, data) break } line := data[:idx] if len(line) > 0 && line[len(line)-1] == '\r' { line = line[:len(line)-1] } lines = append(lines, line) data = data[idx+1:] } return lines } // SplitTabs splits a line on '\t'. func SplitTabs(line []byte) [][]byte { return bytes.Split(line, []byte{'\t'}) } // SplitTabString splits a string on '\t'. func SplitTabString(s string) []string { return bytesToStrings(bytes.Split([]byte(s), []byte{'\t'})) } func bytesToStrings(bs [][]byte) []string { out := make([]string, len(bs)) for i, b := range bs { out[i] = string(b) } return out }