tokenizer.h

tokenizer.h

#include <google/protobuf/io/tokenizer.h>
namespace google::protobuf::io

Class for parsing tokenized text from a ZeroCopyInputStream.

Classes in this file

Abstract interface for an object which collects the errors that occur during parsing.
This class converts a stream of raw text into a stream of tokens for the protocol definition parser to parse.
Structure representing a token read from the token stream.

class ErrorCollector

#include <google/protobuf/io/tokenizer.h>
namespace google::protobuf::io

Abstract interface for an object which collects the errors that occur during parsing.

A typical implementation might simply print the errors to stdout.

Members

ErrorCollector()
virtual
~ErrorCollector()
virtual void
AddError(int line, int column, const string & message) = 0
Indicates that there was an error in the input at the given line and column numbers.  more...
virtual void
AddWarning(int line, int column, const string & message)
Indicates that there was a warning in the input at the given line and column numbers.  more...

virtual void ErrorCollector::AddError(
        int line,
        int column,
        const string & message) = 0

Indicates that there was an error in the input at the given line and column numbers.

The numbers are zero-based, so you may want to add 1 to each before printing them.


virtual void ErrorCollector::AddWarning(
        int line,
        int column,
        const string & message)

Indicates that there was a warning in the input at the given line and column numbers.

The numbers are zero-based, so you may want to add 1 to each before printing them.

class Tokenizer

#include <google/protobuf/io/tokenizer.h>
namespace google::protobuf::io

This class converts a stream of raw text into a stream of tokens for the protocol definition parser to parse.

The tokens recognized are similar to those that make up the C language; see the TokenType enum for precise descriptions. Whitespace and comments are skipped. By default, C- and C++-style comments are recognized, but other styles can be used by calling set_comment_style().

Members

enum
TokenType
enum
CommentStyle
Valid values for  set_comment_style()more...
Tokenizer(ZeroCopyInputStream * input, ErrorCollector * error_collector)
Construct a  Tokenizer that reads and tokenizes text from the given input stream and writes errors to the given error_collector. more...
~Tokenizer()
const Token&
current()
Get the current token.  more...
const Token&
previous()
Return the previous token -- i.e.  more...
bool
Next()
Advance to the next token.  more...
bool
NextWithComments(string * prev_trailing_comments, vector< string > * detached_comments, string * next_leading_comments)
Like  Next(), but also collects comments which appear between the previous and next tokens.  more...
void
set_allow_f_after_float(bool value)
Set true to allow floats to be suffixed with the letter 'f'.  more...
void
set_comment_style(CommentStyle style)
Sets the comment style.

Parse helpers

static double
ParseFloat(const string & text)
Parses a TYPE_FLOAT token.  more...
static void
ParseString(const string & text, string * output)
Parses a TYPE_STRING token.  more...
static void
ParseStringAppend(const string & text, string * output)
Identical to ParseString, but appends to output.
static bool
ParseInteger(const string & text, uint64 max_value, uint64 * output)
Parses a TYPE_INTEGER token.  more...

enum Tokenizer::TokenType {
  TYPE_START,
  TYPE_END,
  TYPE_IDENTIFIER,
  TYPE_INTEGER,
  TYPE_FLOAT,
  TYPE_STRING,
  TYPE_SYMBOL
}

TYPE_START Next() has not yet been called.
TYPE_END End of input reached. "text" is empty.
TYPE_IDENTIFIER

A sequence of letters, digits, and underscores, not starting with a digit.

It is an error for a number to be followed by an identifier with no space in between.

TYPE_INTEGER

A sequence of digits representing an integer.

Normally the digits are decimal, but a prefix of "0x" indicates a hex number and a leading zero indicates octal, just like with C numeric literals. A leading negative sign is NOT included in the token; it's up to the parser to interpret the unary minus operator on its own.

TYPE_FLOAT

A floating point literal, with a fractional part and/or an exponent.

Always in decimal. Again, never negative.

TYPE_STRING

A quoted sequence of escaped characters.

Either single or double quotes can be used, but they must match. A string literal cannot cross a line break.

TYPE_SYMBOL

Any other printable character, like '!' or '+'.

Symbols are always a single character, so "!+$%" is four tokens.


enum Tokenizer::CommentStyle {
  CPP_COMMENT_STYLE,
  SH_COMMENT_STYLE
}

Valid values for set_comment_style().

CPP_COMMENT_STYLE Line comments begin with "//", block comments are delimited by "/*" and "* /".
SH_COMMENT_STYLE Line comments begin with "#". No way to write block comments.

Tokenizer::Tokenizer(
        ZeroCopyInputStream * input,
        ErrorCollector * error_collector)

Construct a Tokenizer that reads and tokenizes text from the given input stream and writes errors to the given error_collector.

The caller keeps ownership of input and error_collector.


const Token & Tokenizer::current()

Get the current token.

This is updated when Next() is called. Before the first call to Next()current() has type TYPE_START and no contents.


const Token & Tokenizer::previous()

Return the previous token -- i.e.

what current() returned before the previous call to Next().


bool Tokenizer::Next()

Advance to the next token.

Returns false if the end of the input is reached.


bool Tokenizer::NextWithComments(
        string * prev_trailing_comments,
        vector< string > * detached_comments,
        string * next_leading_comments)

Like Next(), but also collects comments which appear between the previous and next tokens.

Comments which appear to be attached to the previous token are stored in *prev_tailing_comments. Comments which appear to be attached to the next token are stored in *next_leading_comments. Comments appearing in between which do not appear to be attached to either will be added to detached_comments. Any of these parameters can be NULL to simply discard the comments.

A series of line comments appearing on consecutive lines, with no other tokens appearing on those lines, will be treated as a single comment.

Only the comment content is returned; comment markers (e.g. //) are stripped out. For block comments, leading whitespace and an asterisk will be stripped from the beginning of each line other than the first. Newlines are included in the output.

Examples:

optional int32 foo = 1;  // Comment attached to foo.
 Comment attached to bar.
optional int32 bar = 2;

optional string baz = 3;
 Comment attached to baz.
 Another line attached to baz.

 Comment attached to qux.

 Another line attached to qux.
optional double qux = 4;

 Detached comment.  This is not attached to qux or corge
 because there are blank lines separating it from both.

optional string corge = 5;
/* Block comment attached
 to corge.  Leading asterisks
 will be removed. * /
/* Block comment attached to
 grault. * /
optional int32 grault = 6;

void Tokenizer::set_allow_f_after_float(
        bool value)

Set true to allow floats to be suffixed with the letter 'f'.

Tokens which would otherwise be integers but which have the 'f' suffix will be forced to be interpreted as floats. For all other purposes, the 'f' is ignored.


static double Tokenizer::ParseFloat(
        const string & text)

Parses a TYPE_FLOAT token.

This never fails, so long as the text actually comes from a TYPE_FLOAT token parsed by Tokenizer. If it doesn't, the result is undefined (possibly an assert failure).


static void Tokenizer::ParseString(
        const string & text,
        string * output)

Parses a TYPE_STRING token.

This never fails, so long as the text actually comes from a TYPE_STRING token parsed by Tokenizer. If it doesn't, the result is undefined (possibly an assert failure).


static bool Tokenizer::ParseInteger(
        const string & text,
        uint64 max_value,
        uint64 * output)

Parses a TYPE_INTEGER token.

Returns false if the result would be greater than max_value. Otherwise, returns true and sets *output to the result. If the text is not from a Token of type TYPE_INTEGER originally parsed by a Tokenizer, the result is undefined (possibly an assert failure).

struct Tokenizer::Token

#include <google/protobuf/io/tokenizer.h>
namespace google::protobuf::io

Structure representing a token read from the token stream.

Members

TokenType
type
string
text
The exact text of the token as it appeared in the input.  more...
int
line
"line" and "column" specify the position of the first character of the token within the input stream.  more...
int
column
int
end_column

stringToken::text

The exact text of the token as it appeared in the input.

e.g. tokens of TYPE_STRING will still be escaped and in quotes.


intToken::line

"line" and "column" specify the position of the first character of the token within the input stream.

They are zero-based.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值