Involved Source Filesdecode.godecode_number.godecode_string.godecode_token.go
Package text implements the text format for protocol buffers.
This package has no semantic understanding for protocol buffers and is only
a parser and composer for the format.
There is no formal specification for the protobuf text format, as such the
C++ implementation (see google::protobuf::TextFormat) is the reference
implementation of the text format.
This package is neither a superset nor a subset of the C++ implementation.
This implementation permits a more liberal grammar in some cases to be
backwards compatible with the historical Go implementation.
Future parsings unique to Go should not be added.
Some grammars allowed by the C++ implementation are deliberately
not implemented here because they are considered a bug by the protobuf team
and should not be replicated.
The Go implementation should implement a sufficient amount of the C++
grammar such that the default text serialization by C++ can be parsed by Go.
However, just because the C++ parser accepts some input does not mean that
the Go implementation should as well.
The text format is almost a superset of JSON except:
* message keys are not quoted strings, but identifiers
* the top-level value must be a message without the delimiters
encode.go
Package-Level Type Names (total 9, in which 5 are exported)
/* sort exporteds by: | */
Decoder is a token-based textproto decoder.
in contains the unconsumed input.
lastCall is last method called, either readCall or peekCall.
Initial value is readCall.
lastErr contains the last read error.
lastToken contains the last read token.
openStack is a stack containing the byte characters for MessageOpen and
ListOpen kinds. The top of stack represents the message or the list that
the current token is nested in. An empty stack means the current token is
at the top level message. The characters '{' and '<' both represent the
MessageOpen kind.
orig is used in reporting line and column.
Peek looks ahead and returns the next token and error without advancing a read.
Position returns line and column number of given index of the original input.
It will panic if index is out of range.
Read returns the next token.
It will return an error if there is no valid token.
consume consumes n bytes of input and any subsequent whitespace or comments.
consumeToken constructs a Token for given Kind from d.in and consumes given
size-length from it.
currentOpenKind indicates whether current position is inside a message, list
or top-level message by returning MessageOpen, ListOpen or bof respectively.
If the returned kind is either a MessageOpen or ListOpen, it also returns the
corresponding closing character.
newSyntaxError returns a syntax error with line and column information for
current position.
parseFieldName parses field name and separator.
parseLiteralValue parses a literal value. A literal value is used for
bools, special floats and enums. This function simply identifies that the
field value is a literal.
parseNext parses the next Token based on given last kind.
parseNumberValue parses a number from the input and returns a Token object.
parseScalar parses for a string, literal or number value.
parseString parses a string value enclosed in " or '.
parseStringValue parses string field token.
This differs from parseString since the text format allows
multiple back-to-back string literals where they are semantically treated
as a single large string with all values concatenated.
E.g., `"foo" "bar" "baz"` => "foobarbaz"
parseTypeName parses Any type URL or extension field name. The name is
enclosed in [ and ] characters. The C++ parser does not handle many legal URL
strings. This implementation is more liberal and allows for the pattern
^[-_a-zA-Z0-9]+([./][-_a-zA-Z0-9]+)*`). Whitespaces and comments are allowed
in between [ ], '.', '/' and the sub names.
(*T) popOpenStack()(*T) pushOpenStack(ch byte)(*T) tryConsumeChar(c byte) bool
func NewDecoder(b []byte) *Decoder
Encoder provides methods to write out textproto constructs and values. The user is
responsible for producing valid sequences of constructs and values.
delims[2]byteencoderStateencoderStateencoderState.indents[]byteencoderState.lastTypeencTypeencoderState.out[]byteindentstringoutputASCIIbool
Bytes returns the content of the written bytes.
EndMessage writes out the '}' or '>' symbol.
Reset resets the Encoder to the given encoderState from a Snapshot.
Snapshot returns the current snapshot for use in Reset.
StartMessage writes out the '{' or '<' symbol.
WriteBool writes out the given boolean value.
WriteFloat writes out the given float value for given bitSize.
WriteInt writes out the given signed integer value.
WriteLiteral writes out the given string as a literal value without quotes.
This is used for writing enum literal strings.
WriteName writes out the field name and the separator ':'.
WriteString writes out the given string value.
WriteUint writes out the given unsigned integer value.
prepareNext adds possible space and indentation for the next value based
on last encType and indent option. It also updates e.lastType to next.
func NewEncoder(indent string, delims [2]byte, outputASCII bool) (*Encoder, error)
Token provides a parsed token kind and value. Values are provided by the
different accessor methods.
attrs contains metadata for the following Kinds:
Name: hasSeparator bit and one of NameKind.
Scalar: one of numberValue, stringValue, literalValue.
Kind of the Token object.
numAttrs contains metadata for numberValue:
- highest bit is whether negative or positive.
- lower bits indicate one of numDec, numHex, numOct, numFloat.
pos provides the position of the token in the original input.
raw bytes of the serialized token.
This is a subslice into the original input.
str contains parsed string for the following:
- stringValue of Scalar kind
- numberValue of Scalar kind
- TypeName of Name kind
Bool returns the bool value for a Scalar type.
Enum returns the literal value for a Scalar type for use as enum literals.
FieldNumber returns the value for FieldNumber type. It returns a
non-negative int32 value. Caller will still need to validate for the correct
field number range.
Float32 returns the float32 value for a Scalar type.
Float64 returns the float64 value for a Scalar type.
HasSeparator returns true if the field name is followed by the separator char
':', else false. It panics if type is not Name.
IdentName returns the value for IdentName type.
Int32 returns the int32 value for a Scalar type.
Int64 returns the int64 value for a Scalar type.
Kind returns the token kind.
NameKind returns IdentName, TypeName or FieldNumber.
It panics if type is not Name.
Pos returns the token position from the input.
RawString returns the read value in string.
String returns the string value for a Scalar type.
TypeName returns the value for TypeName type.
Uint32 returns the uint32 value for a Scalar type.
Uint64 returns the uint64 value for a Scalar type.
func (*Decoder).Peek() (Token, error)
func (*Decoder).Read() (Token, error)
func (*Decoder).consumeToken(kind Kind, size int, attrs uint8) Token
func (*Decoder).parseFieldName() (tok Token, err error)
func (*Decoder).parseLiteralValue() (Token, bool)
func (*Decoder).parseNext(lastKind Kind) (Token, error)
func (*Decoder).parseNumberValue() (Token, bool)
func (*Decoder).parseScalar() (Token, error)
func (*Decoder).parseStringValue() (Token, error)
func (*Decoder).parseTypeName() (Token, error)
func TokenEquals(x, y Token) bool
call specifies which Decoder method was invoked.
const peekCall
const readCall
number is the result of parsing out a valid number from parseNumber. It
contains data for doing float or integer conversion via the strconv package
in conjunction with the input bytes.
kinduint8negboolsizeint
func parseNumber(input []byte) number
Package-Level Functions (total 14, in which 4 are exported)
NewDecoder returns a Decoder to read the given []byte.
NewEncoder returns an Encoder.
If indent is a non-empty string, it causes every entry in a List or Message
to be preceded by the indent and trailed by a newline.
If delims is not the zero value, it controls the delimiter characters used
for messages (e.g., "{}" vs "<>").
If outputASCII is true, strings will be serialized in such a way that
multi-byte UTF-8 sequences are escaped. This property ensures that the
overall output is ASCII (as opposed to UTF-8).
TokenEquals returns true if given Tokens are equal, else false.
UnmarshalString returns an unescaped string given a textproto string value.
String value needs to contain single or double quotes. This is only used by
internal/encoding/defval package for unmarshaling bytes.
parseIdent parses an unquoted proto identifier and returns size.
If allowNeg is true, it allows '-' to be the first character in the
identifier. This is used when parsing literal values like -infinity, etc.
Regular expression matches an identifier: `^[_a-zA-Z][_a-zA-Z0-9]*`
parseNumber constructs a number object from given input. It allows for the
following patterns:
integer: ^-?([1-9][0-9]*|0[xX][0-9a-fA-F]+|0[0-7]*)
float: ^-?((0|[1-9][0-9]*)?([.][0-9]*)?([eE][+-]?[0-9]+)?[fF]?)
It also returns the number of parsed bytes for the given number, 0 if it is
not a number.
Package-Level Variables (total 5, in which 1 are exported)
ErrUnexpectedEOF means that EOF was encountered in the middle of the input.
These exact boolean literals are the ones supported in C++.
Any sequence that looks like a non-delimiter (for error reporting).
These are the supported float literals which C++ permits case-insensitive
variants of these.
Package-Level Constants (total 31, in which 11 are exported)
Kind values.
NameKind values.
NameKind values.
Kind values.
Kind values.
Kind values.
Kind values.
Kind values.
Kind values.
Kind values.
NameKind values.
bof indicates beginning of file, which is the default token
kind at the beginning of parsing.
comma and semi-colon are only for parsing in between values and should not be exposed.
Bit mask in Token.attrs to indicate if a Name token is followed by the
separator char ':'. The field name separator char is optional for message
field or repeated message field, but required for all other types. Decoder
simply indicates whether a Name token is followed by separator or not. It is
up to the prototext package to validate.
Bit mask in Token.numAttrs to indicate that the number is a negative.
The pages are generated with Goldsv0.3.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.