"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/http_prompt/lexer.py" (5 Mar 2021, 6012 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "lexer.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.11.2_vs_1.0.0.

    1 from pygments.lexer import (RegexLexer, bygroups, words, using, include,
    2                             combined)
    3 from pygments.lexers import BashLexer
    4 
    5 from pygments.token import Text, String, Keyword, Name, Operator
    6 
    7 from . import options as opt
    8 
    9 
   10 __all__ = ['HttpPromptLexer']
   11 
   12 
   13 FLAG_OPTIONS = [name for name, _ in opt.FLAG_OPTIONS]
   14 VALUE_OPTIONS = [name for name, _ in opt.VALUE_OPTIONS]
   15 HTTP_METHODS = ('get', 'head', 'post', 'put', 'patch', 'delete', 'options', 'connect')
   16 
   17 
   18 def string_rules(state):
   19     return [
   20         (r'(")((?:[^\r\n"\\]|(?:\\.))+)(")',
   21          bygroups(Text, String, Text), state),
   22 
   23         (r'(")((?:[^\r\n"\\]|(?:\\.))+)', bygroups(Text, String), state),
   24 
   25         (r"(')((?:[^\r\n'\\]|(?:\\.))+)(')",
   26          bygroups(Text, String, Text), state),
   27 
   28         (r"(')((?:[^\r\n'\\]|(?:\\.))+)", bygroups(Text, String), state),
   29 
   30         (r'([^\s\'\\]|(\\.))+', String, state)
   31     ]
   32 
   33 
   34 class HttpPromptLexer(RegexLexer):
   35 
   36     name = 'HttpPrompt'
   37     aliases = ['http-prompt']
   38     filenames = ['*.http-prompt']
   39 
   40     tokens = {
   41         'root': [
   42             (r'\s+', Text),
   43             (r'(cd)(\s*)', bygroups(Keyword, Text), 'cd'),
   44             (r'(rm)(\s*)', bygroups(Keyword, Text), 'rm_option'),
   45             (r'(httpie|curl)(\s*)', bygroups(Keyword, Text), 'action'),
   46 
   47             (words(HTTP_METHODS, prefix='(?i)', suffix='(?!\S)(\s*)'),
   48              bygroups(Keyword, Text), combined('redir_out', 'urlpath')),
   49             
   50             (r'(clear)(\s*)', bygroups(Keyword, Text), 'end'),
   51             (r'(exit)(\s*)', bygroups(Keyword, Text), 'end'),
   52             (r'(help)(\s)*', bygroups(Keyword, Text), 'end'),
   53             (r'(env)(\s*)', bygroups(Keyword, Text),
   54              combined('redir_out', 'pipe')),
   55             (r'(source)(\s*)', bygroups(Keyword, Text), 'file_path'),
   56             (r'(exec)(\s*)', bygroups(Keyword, Text), 'file_path'),
   57             (r'(ls)(\s*)', bygroups(Keyword, Text),
   58              combined('redir_out', 'urlpath')),
   59             (r'', Text, 'concat_mut')
   60         ],
   61 
   62         'cd': string_rules('end'),
   63 
   64         'rm_option': [
   65             (r'(\-(?:h|o|b|q))(\s*)', bygroups(Name, Text), 'rm_name'),
   66             (r'(\*)(\s*)', bygroups(Name, Text), 'end')
   67         ],
   68         'rm_name': string_rules('end'),
   69 
   70         'shell_command': [
   71             (r'(`)([^`]*)(`)', bygroups(Text, using(BashLexer), Text)),
   72         ],
   73         'pipe': [
   74             (r'(\s*)(\|)(.*)', bygroups(Text, Operator, using(BashLexer))),
   75         ],
   76         'concat_mut': [
   77             (r'$', Text, 'end'),
   78             (r'\s+', Text),
   79 
   80             # Flag options, such as (--form) and (--json)
   81             (words(FLAG_OPTIONS, suffix=r'\b'), Name, 'concat_mut'),
   82 
   83             # Options with values, such as (--style=default) and (--pretty all)
   84             (words(VALUE_OPTIONS, suffix=r'\b'), Name,
   85              combined('shell_command', 'option_op')),
   86 
   87             include('shell_command'),
   88 
   89             # Unquoted or value-quoted request mutation,
   90             # such as (name="John Doe") and (name=John\ Doe)
   91             (r'((?:[^\s\'"\\=:]|(?:\\.))*)(:=|:|==|=)',
   92              bygroups(Name, Operator),
   93              combined('shell_command', 'unquoted_mut')),
   94 
   95             # Full single-quoted request mutation, such as ('name=John Doe')
   96             (r"(')((?:[^\r\n'\\=:]|(?:\\.))+)(:=|:|==|=)",
   97              bygroups(Text, Name, Operator),
   98              combined('shell_command', 'squoted_mut')),
   99 
  100             # Full double-quoted request mutation, such as ("name=John Doe")
  101             (r'(")((?:[^\r\n"\\=:]|(?:\\.))+)(:=|:|==|=)',
  102              bygroups(Text, Name, Operator),
  103              combined('shell_command', 'dquoted_mut'))
  104         ],
  105 
  106         'option_op': [
  107             (r'(\s+|=)', Operator, 'option_value'),
  108         ],
  109         'option_value': string_rules('#pop:2'),
  110         'file_path': string_rules('end'),
  111         'redir_out': [
  112             (r'(?i)(>>?)(\s*)', bygroups(Operator, Text), 'file_path')
  113         ],
  114 
  115         'unquoted_mut': string_rules('#pop'),
  116         'squoted_mut': [
  117             (r"((?:[^\r\n'\\]|(?:\\.))+)(')", bygroups(String, Text), '#pop'),
  118             (r"([^\r\n'\\]|(\\.))+", String, '#pop')
  119         ],
  120         'dquoted_mut': [
  121             (r'((?:[^\r\n"\\]|(?:\\.))+)(")', bygroups(String, Text), '#pop'),
  122             (r'([^\r\n"\\]|(\\.))+', String, '#pop')
  123         ],
  124 
  125         'action': [
  126             (words(HTTP_METHODS, prefix='(?i)', suffix='(\s*)'),
  127              bygroups(Keyword, Text),
  128              combined('redir_out', 'pipe', 'urlpath')),
  129             (r'', Text, combined('redir_out', 'pipe', 'urlpath'))
  130         ],
  131         'urlpath': [
  132             (r'https?://([^\s"\'\\]|(\\.))+', String,
  133              combined('concat_mut', 'redir_out', 'pipe')),
  134 
  135             (r'(")(https?://(?:[^\r\n"\\]|(?:\\.))+)(")',
  136              bygroups(Text, String, Text),
  137              combined('concat_mut', 'redir_out', 'pipe')),
  138 
  139             (r'(")(https?://(?:[^\r\n"\\]|(?:\\.))+)',
  140              bygroups(Text, String)),
  141 
  142             (r"(')(https?://(?:[^\r\n'\\]|(?:\\.))+)(')",
  143              bygroups(Text, String, Text),
  144              combined('concat_mut', 'redir_out', 'pipe')),
  145 
  146             (r"(')(https?://(?:[^\r\n'\\]|(?:\\.))+)",
  147              bygroups(Text, String)),
  148 
  149             (r'(")((?:[^\r\n"\\=:]|(?:\\.))+)(")',
  150              bygroups(Text, String, Text),
  151              combined('concat_mut', 'redir_out', 'pipe')),
  152 
  153             (r'(")((?:[^\r\n"\\=:]|(?:\\.))+)', bygroups(Text, String)),
  154 
  155             (r"(')((?:[^\r\n'\\=:]|(?:\\.))+)(')",
  156              bygroups(Text, String, Text),
  157              combined('concat_mut', 'redir_out', 'pipe')),
  158 
  159             (r"(')((?:[^\r\n'\\=:]|(?:\\.))+)", bygroups(Text, String)),
  160 
  161             (r'([^\-](?:[^\s"\'\\=:]|(?:\\.))+)(\s+|$)',
  162              bygroups(String, Text),
  163              combined('concat_mut', 'redir_out', 'pipe')),
  164 
  165             (r'', Text,
  166              combined('concat_mut', 'redir_out', 'pipe'))
  167         ],
  168 
  169         'end': [
  170             (r'\n', Text, 'root')
  171         ]
  172     }