In-depth: The Scanner 深入:扫描器 |
Basic Scanner API
基本扫描器API
class scanner | |||||||||
value_t | typedef: The value type of the scanner's iterator typedef,扫描器的迭代器的值类型 | ||||||||
ref_t | typedef: The reference type of the scanner's iterator typedef,扫描器迭代器的引用类型 | ||||||||
bool at_end() const | Returns true if the input is exhausted 如果输入耗尽则返回真 | ||||||||
value_t operator*() const | Dereference/get a 从输入解引用/取得一个value_t | ||||||||
scanner const& operator++() | move the scanner forward 使扫描器步进 | ||||||||
IteratorT& first | The iterator pointing to the current input position. Held by reference 指向当前位置的迭代器,以引用的方式使用。 | ||||||||
IteratorT const last | The iterator pointing to the end of the input. Held by value 指向输入终点的迭代器,以值的方式使用。 |
The basic behavior of the scanner is handled by policies. The actual execution of the scanner's public member functions listed in the table above is implemented by the scanner policies.
扫描器的基本行为是通过策略执行的。上表中列出的扫描器的public成员函数的真正执行是由扫描器策略来实现的。
Three sets of policies govern the behavior of the scanner. These policies make it possible to extend Spirit non-intrusively. The scanner policies allow the core-functionality to be extended without requiring any potentially destabilizing changes to the code. A library writer might provide her own policies that override the ones that are already in place to fine tune the parsing process to fit her own needs. Layers above the core might also want to take advantage of this policy based machanism. Abstract syntax tree generation, debuggers and lexers come to mind.
三类策略决定了扫描器的行为。这些策略使得非侵犯性地扩展Spirit成为可能。扫描器的策略使得从根本上不必对原有代码进行修改就可以扩展其核心功能。一个库的编写者可以用他自己编写的策略覆盖原有的策略来调整分析过程以适应自己的需要。核心之上的层次也有可能从基于策略的设计方式获得好处。抽象语法树的生成、调试器、词法分析器等等就是这种情况。
There are three sets of policies that govern:
三类策略分别管理:
- Iteration and filtering
- 迭代和过滤
- Recognition and matching
- 识别与匹配
- Handling semantic actions
- 处理语义动作
iteration_policy
Here are the default policies that govern iteration and filtering:
这是管理迭代和过滤的默认策略:
struct iteration_policy
{
template <typename ScannerT>
void
advance(ScannerT const& scan) const
{ ++scan.first; }
template <typename ScannerT>
bool at_end(ScannerT const& scan) const
{ return scan.first == scan.last; }
template <typename T>
T filter(T ch) const
{ return ch; }
template <typename ScannerT>
typename ScannerT::ref_t
get(ScannerT const& scan) const
{ return *scan.first; }
};
Iteration and filtering policies 迭代和过滤策略 | |||||||
advance | Move the iterator forward 步进迭代器 | ||||||
at_end | Return true if the input is exhausted 如果输入耗尽则返回真 | ||||||
filter | Filter a character read from the input 过滤从输入中读入的一个字符 | ||||||
get | Read a character from the input 从输入中读取一个字符 |
The following code snippet demonstrates a simple policy that converts all characters to lower case:
下面的代码片断展示了一个把所有字母转为小写的简单策略:
struct inhibit_case_iteration_policy : public iteration_policy
{
template <typename CharT>
CharT filter(CharT ch) const
{
return std::tolower(ch);
}
};
match_policy
Here are the default policies that govern recognition and matching:
这是管理识别和匹配的默认策略:
struct match_policy
{
template <typename T>
struct result
{
typedef match<T> type;
};
const match<nil_t>
no_match() const
{
return match<nil_t>();
}
const match<nil_t>
empty_match() const
{
return match<nil_t>(0, nil_t());
}
template <typename AttrT, typename IteratorT>
match<AttrT>
create_match(
std::size_t length,
AttrT const& val,
IteratorT const& /*first*/,
IteratorT const& /*last*/) const
{
return match<AttrT>(length, val);
}
template <typename MatchT, typename IteratorT>
void
group_match(
MatchT& /*m*/,
parser_id const& /*id*/,
IteratorT const& /*first*/,
IteratorT const& /*last*/) const {}
template <typename Match1T, typename Match2T>
void
concat_match(Match1T& l, Match2T const& r) const
{
l.concat(r);
}
};
Recognition and matching 识别与匹配 | |||||||||||
result | A metafunction that returns a match type given an attribute type (see In-depth: The Parser) 给定属性类型返回匹配类型的元函数(见 深入:分析器) | ||||||||||
no_match | Create a failed match 生成一个失败匹配 | ||||||||||
empty_match | Create an empty match. An empty match is a successful epsilon match (matching length == 0) 生成一个空匹配。空匹配是指成功的空集匹配(匹配长度为0) | ||||||||||
create_match | Create a match given the matching length, an attribute and the iterator pair pointing to the matching portion of the input 生成一个带着匹配长度、匹配属性和指向输入中的匹配段的迭代器对的匹配 | ||||||||||
group_match | For non terminals such as rules, this is called after a successful match has been made to allow post processing 对诸如rule这样的非终结符,这玩意将在成功匹配后调用以进行后续处理 | ||||||||||
concat_match | Concatenate two match objects 合并两个匹配对象 |
action_policy
The action policy has only one function for handling semantic actions:
动作策略只有一个处理语义动作的函数:
struct action_policy
{
template <typename ActorT, typename AttrT, typename IteratorT>
void
do_action(
ActorT const& actor,
AttrT const& val,
IteratorT const& first,
IteratorT const& last) const;
};
The default action policy forwards to:
默认的策略为:
actor(first, last);
If the attribute val is of type nil_t. Otherwise:
如果属性val是nil_t的话。否则:
actor(val);
scanner_policies mixer
scanner_policies 混合器
The class scanner_policies combines the three scanner policy classes above into one:
scanner_policies类把上面三类策略捆绑在了一起:
template <
typename IterationPolicyT = iteration_policy,
typename MatchPolicyT = match_policy,
typename ActionPolicyT = action_policy>
struct scanner_policies;
This mixer class inherits from all the three policies. This scanner_policies class is then used to parameterize the scanner:
这个混合器类同时继承了三个策略。然后这个scanner_policies就被用于将扫描器参数化:
template <
typename IteratorT = char const*,
typename PoliciesT = scanner_policies<> >
class scanner;
The scanner in turn inherits from the PoliciesT.
扫描器接着继承PoliciesT。
Rebinding Policies
策略重绑定
The scanner can be made to rebind to a different set of policies anytime. It has a member function change_policies(new_policies). Given a new set of policies, this member function creates a new scanner with the new set of policies. The result type of the rebound scanner can be can be obtained by calling the metafunction:
扫描器在任何时刻都可以绑定另外的策略。它有一个change_policies(new_policies)成员函数。给定一个新的策略集,这个成员函数产生一个带有给定策略集的新的扫描器。重新绑定的扫描器的结果类可以通过下述元函数取得:
rebind_scanner_policies<ScannerT, PoliciesT>::type
Rebinding Iterators
迭代器重绑定
The scanner can also be made to rebind to a different iterator type anytime. It has a member function change_iterator(first, last). Given a new pair of iterator of type different from the ones held by the scanner, this member function creates a new scanner with the new pair of iterators. The result type of the rebound scanner can be can be obtained by calling the metafunction:
扫描器也可以在任何时刻重新绑定不同的迭代器类。它有一个change_iterator(first, last)成员函数。给定一对与原先的扫描器所持有的迭代器的类型不同的迭代器,这个成员函数将产生一个带有新的迭代器对的扫描器。重绑定的扫描器的结果类可以通过下述元函数取得:
rebind_scanner_iterator<ScannerT, IteratorT>::type
Copyright © 1998-2003 Joel de Guzman
Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)