How to use the sparse.Syntax function in sparse

To help you get started, we’ve selected a few sparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rix0rrr / gcl / gcl / ast2.py View on Github external
# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])


def listMembers(t_sep, expr):
  return sparse.delimited_list(expr, sparse.Q(t_sep))
github rix0rrr / gcl / gcl / ast2.py View on Github external
return dict2tuple(x)


#----------------------------------------------------------------------
#  PARSING

# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
github rix0rrr / gcl / gcl / ast2.py View on Github external
#----------------------------------------------------------------------
#  PARSING

# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])
github rix0rrr / gcl / gcl / ast2.py View on Github external
# Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])


def listMembers(t_sep, expr):
  return sparse.delimited_list(expr, sparse.Q(t_sep))


def braced_list(t_l, t_r, t_sep, expr, allow_missing_close=False):
  """Parse bracketed list.

  Empty list is possible, as is a trailing separator.
  """
  return braced_expression(t_l, listMembers(t_sep, expr), t_r, allow_missing_close=allow_missing_close)
github rix0rrr / gcl / gcl / ast2.py View on Github external
sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])


def listMembers(t_sep, expr):
  return sparse.delimited_list(expr, sparse.Q(t_sep))


def braced_list(t_l, t_r, t_sep, expr, allow_missing_close=False):
  """Parse bracketed list.

  Empty list is possible, as is a trailing separator.
github rix0rrr / gcl / gcl / ast2.py View on Github external
#----------------------------------------------------------------------
#  PARSING

# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])
github rix0rrr / gcl / gcl / ast2.py View on Github external
"""Turn a dict-like object into a Tuple."""
  if isinstance(x, framework.TupleLike):
    return x
  return dict2tuple(x)


#----------------------------------------------------------------------
#  PARSING

# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
github rix0rrr / gcl / gcl / ast2.py View on Github external
scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])


def listMembers(t_sep, expr):
  return sparse.delimited_list(expr, sparse.Q(t_sep))


def braced_list(t_l, t_r, t_sep, expr, allow_missing_close=False):
  """Parse bracketed list.
github rix0rrr / gcl / gcl / ast2.py View on Github external
# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols
    sparse.Syntax('symbol', '[' + ''.join('\\' + s for s in '[](){}=,;:.') + ']'),
    ])


def listMembers(t_sep, expr):
  return sparse.delimited_list(expr, sparse.Q(t_sep))
github rix0rrr / gcl / gcl / ast2.py View on Github external
return x
  return dict2tuple(x)


#----------------------------------------------------------------------
#  PARSING

# Scanning for all of these at once is faster than scanning for them individually.
keywords = ['inherit', 'if', 'then', 'else', 'include', 'null', 'for', 'in', 'private', 'required']

scanner = sparse.Scanner([
    sparse.WHITESPACE,
    sparse.Syntax('comment', '#$|#[^.].*$'),
    sparse.Syntax('doc_comment', '#\.(.*)$', lambda s: s[2:].strip()),
    # Keywords
    sparse.Syntax('bool_op', r'\band\b|\bor\b'),
    sparse.Syntax('minus', r'-(?!\d)'),
    sparse.Syntax('not', r'\bnot\b'),
    sparse.Syntax('keyword', '|'.join(r'\b' + k + r'\b' for k in keywords)),
    sparse.Syntax('bool_literal', r'\btrue\b|\bfalse\b'),
    # Identifiers (must come after keywords for matching priority)
    sparse.Syntax('identifier', sparse.quoted_string_regex('`'), sparse.quoted_string_process),
    sparse.Syntax('identifier', r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'),
    # Other atoms
    sparse.Syntax('string_literal', sparse.quoted_string_regex('"'), sparse.quoted_string_process),
    sparse.Syntax('string_literal', sparse.quoted_string_regex("'"), sparse.quoted_string_process),
    sparse.Syntax('compare_op', '|'.join(['<=', '>=', '==', '!=', '<', '>'])),
    sparse.Syntax('mul_op', '[*/%]'),
    sparse.Syntax('plus', '\+'),
    sparse.Syntax('float_literal', r'-?\d*\.\d+'),
    sparse.Syntax('int_literal', r'-?\d+'),
    # Symbols