Commits

Alex Gosse committed 53efefa Draft

Add support for Riverbed TrafficScript lexing.

Comments (0)

Files changed (4)

 * Bertrand Goetzmann -- Groovy lexer
 * Krzysiek Goj -- Scala lexer
 * Matt Good -- Genshi, Cheetah lexers
+* Alex Gosse -- TrafficScript lexer
 * Patrick Gotthardt -- PHP namespaces support
 * Olivier Guibe -- Asymptote lexer
 * Jordi Gutiérrez Hermoso -- Octave lexer

pygments/lexers/_mapping.py

     'RhtmlLexer': ('pygments.lexers.templates', 'RHTML', ('rhtml', 'html+erb', 'html+ruby'), ('*.rhtml',), ('text/html+ruby',)),
     'RobotFrameworkLexer': ('pygments.lexers.other', 'RobotFramework', ('RobotFramework', 'robotframework'), ('*.txt', '*.robot'), ('text/x-robotframework',)),
     'RstLexer': ('pygments.lexers.text', 'reStructuredText', ('rst', 'rest', 'restructuredtext'), ('*.rst', '*.rest'), ('text/x-rst', 'text/prs.fallenstein.rst')),
+    'RtsLexer': ('pygments.lexers.trafficscript', 'TrafficScript', ('trafficscript', 'rts'), ('*.rts'), ('text/x-trafficscript', 'application/x-trafficscript')),
     'RubyConsoleLexer': ('pygments.lexers.agile', 'Ruby irb session', ('rbcon', 'irb'), (), ('text/x-ruby-shellsession',)),
     'RubyLexer': ('pygments.lexers.agile', 'Ruby', ('rb', 'ruby', 'duby'), ('*.rb', '*.rbw', 'Rakefile', '*.rake', '*.gemspec', '*.rbx', '*.duby'), ('text/x-ruby', 'application/x-ruby')),
     'RustLexer': ('pygments.lexers.compiled', 'Rust', ('rust',), ('*.rs', '*.rc'), ('text/x-rustsrc',)),

pygments/lexers/traffiscript.py

+# -*- coding: utf-8 -*-
+"""
+
+    pygments.lexers.trafficscript
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+
+    Lexer for RiverBed's TrafficScript (RTS) language.
+
+    :copyright: Copyright 2013 by Alex Gosse <alex.gosse@gmail.com>
+    :license: BSD, see LICENSE for details.
+"""
+import re
+from pygments.lexer import RegexLexer
+from pygments.token import *
+
+__all__ = ['RtsLexer']
+
+class RtsLexer(RegexLexer):
+    """
+    For `Riverbed Stingray Traffic Manager
+    <http://www.riverbed.com/stingray>`
+    """
+    name = 'TrafficScript'
+    aliases = ['rts','trafficscript']
+    filenames = ['*.rts']
+    tokens = {
+        'root' : [
+            (r"'(\\\\|\\[^\\]|[^'\\])*'", String),
+            (r'"', String, 'escapable-string'),
+            (r'(0x[0-9a-fA-F]+|\d+)', Number),
+            (r'\d+\.\d+', Number.Float),
+            (r'\$[a-zA-Z](\w|_)*', Name.Variable),
+            (r'[a-zA-Z][\w.]*', Name.Function),
+            (r'[-+*/%=,;(){}<>^.!~|&\[\]\?\:]', Operator),
+            (r'(>=|<=|==|!=|'
+             r'&&|\|\||'
+             r'\+=|.=|-=|\*=|/=|%=|<<=|>>=|&=|\|=|\^=|'
+             r'>>|<<|'
+             r'\+\+|--|=>)', Operator),
+            (r'[ \t\r]+', Text),
+            (r'#[^\n]*', Comment),
+        ],
+        'escapable-string' : [
+            (r'\\[tsn]', String.Escape),
+            (r'[^"]', String),
+            (r'"', String, '#pop'),
+        ],
+
+    }

tests/examplefiles/example.rts

+# Example of a Riverbed TrafficScript (*.rts) file.
+
+http.setHeader( "Host", "secure.mysite.com" );
+$body = http.getBody( ); # get the POST data
+$single = 'Hello \
+world';
+$double = "Hello \
+world";
+$pi = 3.14157;
+$message = "The URL path is " . http.GetPath();
+$four = 2 + 2;
+# Sets $ratio to "75%" (for example)
+$ratio = ( $a / ($a + $b) * 100 ) . "%";
+$contentLength = http.getHeader( "Content-Length" );
+if( $contentLength > 1024 * 1024 ) {
+  log.warn( "Large request body: ".$contentLength );
+}
+4 + 7.5 * $a
+-$b / $c - 1
+7 % 3    # Returns 1
+"foo" && !0 # true
+( 1 < 2 ) && ( 3 < 4 ) # true
+$a || $b # true if $a or $b is true
+0x1234 & 255         # 0x34
+1|2|4 #7
+1^3 #2
+~1 & 0xffff          # 65534
+1 << 2               # 4
+2 >> 1               # 1
+$foo *= 5 # Product equals ($foo = $foo * 5)
+$foo /= 2 # Quotient equals ($foo = $foo / 5)
+$foo %= 2 # Modulo equals ($foo = $foo % 5)
+$foo <<= 2 # Bit-shift left equals ($foo = $foo << 2)
+$foo >>= 2 # Bit-shift right equals ($foo = $foo >> 2)
+$foo &= 2 # Bitwise AND equals ($foo = $foo & 2)
+$foo |= 2 # Bitwise OR equals ($foo = $foo | 2)
+$foo ^= 2 # Bitwise XOR equals ($foo = $foo ^ 2)
+$int = 10;
+$double = 2.71828;
+string.len( $double ); # casts to string, returns 7
+# Convert $string to a number, and add 4:
+$r = $string + 4;  # $r is 14
+if( string.startsWith( $path, "/secure" ) ) {
+    pool.use( "secure pool" );
+} else {
+    pool.use( "non-secure pool" );
+}
+
+for( $count = 0; $count < 10; $count++ ) {
+    log.info( "In loop, count = " . $count );
+}
+
+i$count = 0;
+while( $count < 10 ) {
+    log.info( "In loop, count = " . $count );
+    $count = $count + 1;
+}
+
+$count = 0;
+do {
+   log.info( "In loop, count = " . $count );
+   $count = $count + 1;
+} while( $count < 10 );
+
+$mime = http.getResponseHeader( "Content-Type" );
+if( !string.startsWith( $mime, "text/html" )) break;
+$array = [ "Alex", "Matt", "Oliver", "Laurence" ];
+$someone = $array[0];
+$arraylen = array.length($array);
+log.info("My array has " . $arraylen . " elements.\n");
+
+for ( $i = 0; $i < $arraylen; $i++ ){
+    log.info ( "Element #" . $i . " " . $array[$i]);
+}
+
+$hash = [ "orange" => "fruit",
+          "apple" => "fruit",
+          "cabbage" => "vegetable",
+          "pear" => "fruit" ];
+
+foreach ( $key in hash.keys($hash)){
+   log.info("Key: " . $key . "; Value: " . $hash[$key] .
+";"); }
+
+# Declare a subroutine to calculate factorials
+sub factorial( $n ) {
+    if( $n == 0 ) return 1;
+       return $n*factorial( $n-1 );
+}
+# Put entries into the array
+$c = 0;
+while( $c <= 10 ) {
+   $msg = "Did you know that ". $c ."! is ". factorial( $c )
+."?" ;
+   data.set( "myarray".$c, $msg );
+$c++; }
+# Look up several entries.  Note: the 1000th entry is empty
+$msg = "";
+$msg .= "Index 1000: ".data.get( "myarray1000" )."\n";
+# delete the entire array (but no other data stored by data.set)
+data.reset( "myarray" );
+http.sendResponse( "200 OK", "text/plain", $msg, "" );
+sub headbug(){
+   # Prints each header to the event log.
+   $headers = http.listHeaderNames();
+   foreach ($header in $headers){
+      log.info( $header . ": " . http.getheader($header));
+} }
+
+import foo;
+foo.headbug();
+# Sets the regex string as ^192\.168\. ; the two examples
+# below have the same effect
+$regex = "^(192)\\.168\\.";
+$regex = '^192\.168\.';
+if ( string.regexMatch( $ip, $regex ) ) {
+    # IP is on 192.168.* network
+}