diff options
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/traffiscript.py | 50 | ||||
-rw-r--r-- | tests/examplefiles/example.rts | 118 |
4 files changed, 170 insertions, 0 deletions
@@ -43,6 +43,7 @@ Other contributors, listed alphabetically, are: * 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 diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 53e09176..37b82318 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -246,6 +246,7 @@ LEXERS = { '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',)), diff --git a/pygments/lexers/traffiscript.py b/pygments/lexers/traffiscript.py new file mode 100644 index 00000000..56b9e04c --- /dev/null +++ b/pygments/lexers/traffiscript.py @@ -0,0 +1,50 @@ +# -*- 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'), + ], + + } diff --git a/tests/examplefiles/example.rts b/tests/examplefiles/example.rts new file mode 100644 index 00000000..1f9cfc59 --- /dev/null +++ b/tests/examplefiles/example.rts @@ -0,0 +1,118 @@ +# 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 +} |