def runTest(self): parser = pypeg2.Parser() r = parser.parse("hello, world", pypeg2.attr("some", pypeg2.Symbol)) self.assertEqual( r, ( ', world', pypeg2.attr.Class(name='some', thing=pypeg2.Symbol('hello'), subtype=None) ) )
class Function(List): grammar = attr('typing', Type), blank, name(), '(', Parameters, ')', endl, block
class DispositionParm(str): """A parameter for the Disposition-Type header (RFC6266, Section 4.1).""" grammar = peg.attr('name', NoExtToken), '=', Value
class KeywordRule(LeafRule): grammar = attr('value', re.compile(r"[\w\d]+(\.[\w\d]+)*"))
class Main(UnaryRule): grammar = [ (omit(_), attr('op', Query), omit(_)), attr('op', EmptyQueryRule), ]
class ImplicitAndQuery(UnaryRule): grammar = [ attr('op', NotQuery), attr('op', ParenthesizedQuery), attr('op', SimpleQuery), ]
class SimpleQuery(UnaryRule): grammar = attr('op', [KeywordQuery, ValueQuery])
re.compile(r'\b(?!\d\d\d\w{0,3}|%s)\S+\b:' % "|".join([x + ":" for x in SPIRES_KEYWORDS.keys()]))) class KeywordQuery(BinaryRule): pass class EmptyQueryRule(LeafRule): grammar = attr('value', re.compile(r'\s*')) KeywordQuery.grammar = [ ( attr('left', KeywordRule), omit(_, Literal(':'), _), # FIXME: This should be replaced with KeywordQuery to restore # intented functionality. # Also NestedKeywordsRule class should be removed from # this file, ./ast.py and ./walkers/pypeg_to_ast.py. attr('right', NestedKeywordsRule) ), ( attr('left', KeywordRule), omit(_, Literal(':'), _), attr('right', Value) ), ( attr('left', KeywordRule), omit(_, Literal(':'), _),
class SpiresKeywordQuery(BinaryRule): """Keyword queries with space separator (i.e. Spires style).""" grammar = attr('left', InspireKeyword), attr('right', Value)
class LessThanOp(UnaryRule): """Less than operator. Supports queries like author-count < 100 or date before 1984. """ grammar = omit(re.compile(r"before|<", re.IGNORECASE)), attr('op', SimpleValue)
class GreaterThanOp(UnaryRule): """Greater than operator. Supports queries like author-count > 2000 or date after 10-2000. """ grammar = omit(re.compile(r"after|>", re.IGNORECASE)), attr('op', SimpleValue)
class SimpleValueNegation(UnaryRule): """Negation accepting only SimpleValues.""" grammar = omit(Not), attr('op', SimpleValue)
class Whitespace(LeafRule): grammar = attr('value', whitespace)
class ParenthesizedQuery(UnaryRule): """Parenthesized query for denoting precedence.""" grammar = omit(Literal('(')), attr('op', Statement), omit(Literal(')')) class NestedKeywordQuery(BinaryRule): """Nested Keyword queries. E.g. citedby:author:hui and refersto:author:witten """ pass Expression.grammar = attr('op', [ NotQuery, NestedKeywordQuery, ParenthesizedQuery, SimpleQuery, ]) NestedKeywordQuery.grammar = \ attr('left', [ # Most specific regex must be higher. re.compile(r'citedbyexcludingselfcites', re.IGNORECASE), re.compile(r'citedbyx', re.IGNORECASE), re.compile(r'citedby', re.IGNORECASE), re.compile(r'referstoexcludingselfcites', re.IGNORECASE), re.compile(r'referstox', re.IGNORECASE), re.compile(r'refersto', re.IGNORECASE), ]), \ optional(omit(":")), \
omit(re.compile(r"or", re.I)), [ (omit(Whitespace), attr('op', SpiresSimpleQuery)), (omit(_), attr('op', SpiresParenthesizedQuery)), (omit(Whitespace), attr('op', SpiresValueQuery)), ] ) SpiresQuery.grammar = attr('children', ( [ SpiresParenthesizedQuery, SpiresSimpleQuery, ], maybe_some(( omit(_), [ SpiresNotQuery, SpiresAndQuery, SpiresOrQuery, ] )), )) class NestableKeyword(LeafRule): grammar = attr('value', [ re.compile('refersto', re.I), re.compile('citedby', re.I), ])
body = None class List(MExpression): head = MSymbol("List") grammar = ( Literal("{"), optional(attr("body", csl(MExpression))), Literal("}") ) # Since MExpression is recursive, we need to define the class, # then the grammar. Moreover, since it depends on List and other # such things, we need to put it last. MExpression.grammar = [ ( attr("head", MSymbol), Literal("["), optional(attr("body", csl(MExpression))), Literal("]") ), attr("head", MSymbol), List, atom ] ## TESTING ########################################################### if __name__ == "__main__": print parse("ab`c", MExpression) print parse('12', MExpression) print parse('"a"', MExpression) print parse("List", MExpression) print parse("List[]", MExpression)
class NotQuery(UnaryRule): """Negation query.""" grammar = omit(Not), attr('op', Expression)
class ParenthesizedQuery(UnaryRule): """Parenthesized query for denoting precedence.""" grammar = omit(Literal('(')), attr('op', Statement), omit(Literal(')'))
class RelativeGroupArgument(IntNumMixin): grammar = ["group"], "=", peg.attr("num", re.compile(r"[+\-][1-4]"))
class ValueQuery(UnaryRule): grammar = attr('op', Value) class Query(ListRule): pass class KeywordQuery(BinaryRule): pass KeywordQuery.grammar = [ ( attr('left', KeywordRule), omit(_, Literal(':'), _), attr('right', KeywordQuery) ), ( attr('left', KeywordRule), omit(_, Literal(':'), _), attr('right', Value) ), ( attr('left', KeywordRule), omit(_, Literal(':'), _), attr('right', Query) ), ]
class ParenthesizedQuery(UnaryRule): grammar = ( omit(Literal('('), _), attr('op', Query), omit(_, Literal(')')), )
class DoubleQuotedString(LeafRule): grammar = Literal('"'), attr('value', re.compile(r'([^"]|\\.)*')), \ Literal('"')
class EmptyQueryRule(LeafRule): grammar = attr('value', re.compile(r'\s*'))
class SlashQuotedString(LeafRule): grammar = Literal('/'), attr('value', re.compile(r"([^/]|\\.)*")), \ Literal('/')
class Whitespace(LeafRule): grammar = attr('value', re.compile(r"\s+"))
class SimpleRangeValue(LeafRule): grammar = attr('value', re.compile(r"([^\s\)\(-]|-+[^\s\)\(>])+"))
class SingleQuotedString(LeafRule): grammar = Literal("'"), attr('value', re.compile(r"([^']|\\.)*")), \ Literal("'")
class RangeValue(UnaryRule): grammar = attr('op', [DoubleQuotedString, SimpleRangeValue])
class Parameter: grammar = attr('typing', Type), blank, name()
class RangeOp(BinaryRule): grammar = ( attr('left', RangeValue), Literal('->'), attr('right', RangeValue) )
class FromImport(object): grammar = "from", blank, attr("name", Package), blank, "import", \ blank, attr("modules", Modules), endl
class NestableKeyword(LeafRule): grammar = attr('value', [ re.compile('refersto', re.I), re.compile('citedby', re.I), ])
AST node for a conjunction. """ grammar = (attr('left', Term), blank, Keyword('and'), blank, attr('right', Expression)) class Disjunction(BinaryNode): """ AST node for a disjunction. """ grammar = (attr('left', Term), blank, Keyword('or'), blank, attr('right', Expression)) Term.grammar = attr('expression', [Grouping, Negation, Clause, Tautology]) Expression.grammar = attr('expression', [Conjunction, Disjunction, Term]) class Visitor(object): """ Decorator for visitor methods on AST nodes. >>> class PrettyPrinter(object): ... visitor = Visitor() ... ... @visitor(Tautology) ... def visit(self, node):
class Number(LeafRule): grammar = attr('value', re.compile(r'\d+'))
class ValueQuery(UnaryRule): grammar = attr("op", Value) class Query(ListRule): pass class KeywordQuery(BinaryRule): pass KeywordQuery.grammar = [ (attr("left", KeywordRule), omit(_, Literal(":"), _), attr("right", KeywordQuery)), (attr("left", KeywordRule), omit(_, Literal(":"), _), attr("right", Value)), (attr("left", KeywordRule), omit(_, Literal(":"), _), attr("right", Query)), ] class SimpleQuery(UnaryRule): grammar = attr("op", [KeywordQuery, ValueQuery]) class ParenthesizedQuery(UnaryRule): grammar = (omit(Literal("("), _), attr("op", Query), omit(_, Literal(")"))) class NotQuery(UnaryRule): grammar = [
class ValueQuery(UnaryRule): grammar = attr('op', Value)
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Data Access Module """ from __future__ import absolute_import, print_function, division import re from pypeg2 import word, attr, List, maybe_some, parse as peg_parse from datacube.model import Range FIELD_NAME = attr(u'field_name', word) NUMBER = re.compile(r"[-+]?(\d*\.\d+|\d+\.\d*|\d+)") # A limited string can be used without quotation marks. LIMITED_STRING = re.compile(r"[a-zA-Z][\w\._-]*") # Inside string quotation marks. Kept simple. We're not supporting escapes or much else yet... STRING_CONTENTS = re.compile(r"[\w\s\._-]*") class Expr(object): def query_repr(self, get_field): """ Return this as a database expression. :type get_field: (str) -> datacube.index.fields.Field :rtype: datacube.index.fields.Expression
def compose(self, parser: Any, grammar: Any = None, attr_of: str = None) -> str: """ Return the Condition as string format :param parser: Parser instance :param grammar: Grammar :param attr_of: Attribute of... """ if type(self.left) is Condition: left = "({0})".format(parser.compose(self.left, grammar=grammar, attr_of=attr_of)) else: left = parser.compose(self.left, grammar=grammar, attr_of=attr_of) if getattr(self, 'op', None): if type(self.right) is Condition: right = "({0})".format(parser.compose(self.right, grammar=grammar, attr_of=attr_of)) else: right = parser.compose(self.right, grammar=grammar, attr_of=attr_of) op = parser.compose(self.op, grammar=grammar, attr_of=attr_of) result = "{0} {1} {2}".format(left, op, right) else: result = left return result Condition.grammar = contiguous(attr('left', [SIG, XHX, CSV, CLTV, ('(', Condition, ')')]), maybe_some(whitespace, attr('op', Operator), whitespace, attr('right', [SIG, XHX, CSV, CLTV, ('(', Condition, ')')])))