diff options
author | gbrandl <devnull@localhost> | 2007-04-25 17:06:07 +0200 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2007-04-25 17:06:07 +0200 |
commit | 41139bf9c8fdbe85cb27ec44abccd188b15ca6a8 (patch) | |
tree | d96b74ec8a274b3646b6fd2cd547f940c377f658 | |
parent | 1b68b4671ab60608e2d308b0372b0629b3783360 (diff) | |
download | pygments-41139bf9c8fdbe85cb27ec44abccd188b15ca6a8.tar.gz |
[svn] Make mapfiles, make check, add D test file, make reindent.
-rw-r--r-- | pygments/formatters/bbcode.py | 2 | ||||
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/compiled.py | 23 | ||||
-rw-r--r-- | pygments/lexers/other.py | 1 | ||||
-rw-r--r-- | pygments/util.py | 2 | ||||
-rw-r--r-- | tests/examplefiles/test.d | 135 | ||||
-rw-r--r-- | tests/run.py | 5 | ||||
-rw-r--r-- | tests/test_html_formatter.py | 2 | ||||
-rw-r--r-- | tests/test_token.py | 2 | ||||
-rw-r--r-- | tests/test_util.py | 13 |
10 files changed, 164 insertions, 22 deletions
diff --git a/pygments/formatters/bbcode.py b/pygments/formatters/bbcode.py index 676bd2ea..8714294d 100644 --- a/pygments/formatters/bbcode.py +++ b/pygments/formatters/bbcode.py @@ -36,7 +36,7 @@ class BBCodeFormatter(Formatter): `style` The style to use, can be a string or a Style subclass (default: ``'default'``). - + `codetag` If set to true, put the output into ``[code]`` tags (default: ``false``) diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 1a5b61be..56ee9d71 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -30,6 +30,7 @@ LEXERS = { 'CssLexer': ('pygments.lexers.web', 'CSS', ('css',), ('*.css',), ('text/css',)), 'CssPhpLexer': ('pygments.lexers.templates', 'CSS+PHP', ('css+php',), (), ('text/css+php',)), 'CssSmartyLexer': ('pygments.lexers.templates', 'CSS+Smarty', ('css+smarty',), (), ('text/css+smarty',)), + 'DLexer': ('pygments.lexers.compiled', 'D', (), ('*.d', '*.di'), ()), 'DelphiLexer': ('pygments.lexers.compiled', 'Delphi', ('delphi', 'pas', 'pascal', 'objectpascal'), ('*.pas',), ('text/x-pascal',)), 'DiffLexer': ('pygments.lexers.text', 'Diff', ('diff',), ('*.diff', '*.patch'), ('text/x-diff', 'text/x-patch')), 'DjangoLexer': ('pygments.lexers.templates', 'Django/Jinja', ('django', 'jinja'), (), ('application/x-django-templating', 'application/x-jinja')), diff --git a/pygments/lexers/compiled.py b/pygments/lexers/compiled.py index cb9ef6d0..18d0c069 100644 --- a/pygments/lexers/compiled.py +++ b/pygments/lexers/compiled.py @@ -7,8 +7,7 @@ :copyright: 2006-2007 by Georg Brandl, Armin Ronacher, Christoph Hack, Whitney Young, Kirk McDonald. - :license: BSD, see LICENSE for more details, the D lexer is licensed - under the MIT license and was contributed by Kirk McDonald. + :license: BSD, see LICENSE for more details. """ import re @@ -239,9 +238,11 @@ class DLexer(RegexLexer): (r'(macro)', Keyword.Reserved), # FloatLiteral # -- HexFloat - (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float), + (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)' + r'[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float), # -- DecimalFloat - (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float), + (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|' + r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float), (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?', Number.Float), # IntegerLiteral # -- Binary @@ -1015,7 +1016,7 @@ class ObjectiveCLexer(RegexLexer): r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name r'(\s*\([^;]*?\))' # signature r'(' + _ws + r')(;)', - bygroups(using(this), Name.Function, using(this), Text, Punctuation)), + bygroups(using(this), Name.Function, using(this), Text, Punctuation)), (r'(@interface|@implementation)(\s+)', bygroups(Keyword, Text), 'classname'), (r'(@class|@protocol)(\s+)', bygroups(Keyword, Text), 'forward_classname'), (r'(\s*)(@end)(\s*)', bygroups(Text, Keyword, Text)), @@ -1023,15 +1024,19 @@ class ObjectiveCLexer(RegexLexer): ], 'classname' : [ # interface definition that inherits - ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*:\s*)([a-zA-Z_][a-zA-Z0-9_]*)?', bygroups(Name.Class, Text, Name.Class), '#pop'), + ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*:\s*)([a-zA-Z_][a-zA-Z0-9_]*)?', + bygroups(Name.Class, Text, Name.Class), '#pop'), # interface definition for a category - ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(\([a-zA-Z_][a-zA-Z0-9_]\)*)', bygroups(Name.Class, Text, Name.Label), '#pop'), + ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(\([a-zA-Z_][a-zA-Z0-9_]\)*)', + bygroups(Name.Class, Text, Name.Label), '#pop'), # simple interface / implementation ('([a-zA-Z_][a-zA-Z0-9_]*)', Name.Class, '#pop') ], 'forward_classname' : [ - ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*,\s*)', bygroups(Name.Class, Text), 'forward_classname'), - ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*;?)', bygroups(Name.Class, Text), '#pop') + ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*,\s*)', + bygroups(Name.Class, Text), 'forward_classname'), + ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*;?)', + bygroups(Name.Class, Text), '#pop') ], 'statement' : [ include('whitespace'), diff --git a/pygments/lexers/other.py b/pygments/lexers/other.py index 74ffd41e..d7e45a94 100644 --- a/pygments/lexers/other.py +++ b/pygments/lexers/other.py @@ -338,4 +338,3 @@ class RedcodeLexer(RegexLexer): (r'[-+]?\d+', Number.Integer), ], } - diff --git a/pygments/util.py b/pygments/util.py index 3331400a..463f8468 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -60,7 +60,7 @@ def get_int_opt(options, optname, default=None): except TypeError: raise OptionError('Invalid type %r for option %s; you ' 'must give an integer value' % ( - string, optname)) + string, optname)) except ValueError: raise OptionError('Invalid value %r for option %s; you ' 'must give an integer value' % ( diff --git a/tests/examplefiles/test.d b/tests/examplefiles/test.d new file mode 100644 index 00000000..02fe8f73 --- /dev/null +++ b/tests/examplefiles/test.d @@ -0,0 +1,135 @@ +// Created by Lionello Lunesu and placed in the public domain. +// This file has been modified from its original version. +// It has been formatted to fit your screen. +module phoneno; // optional +import std.stdio; // writefln +import std.ctype; // isdigit +import std.stream; // BufferedFile + +// Just for readability (imagine char[][][char[]]) +alias char[] string; +alias string[] stringarray; + +/// Strips non-digit characters from the string (COW) +string stripNonDigit( in string line ) +{ + string ret; + foreach(uint i, c; line) { + // Error: std.ctype.isdigit at C:\dmd\src\phobos\std\ctype.d(37) + // conflicts with std.stream.isdigit at C:\dmd\src\phobos\std\stream.d(2924) + if (!std.ctype.isdigit(c)) { + if (!ret) + ret = line[0..i]; + } + else if (ret) + ret ~= c; + } + return ret?ret:line; +} + +unittest { + assert( stripNonDigit("asdf") == "" ); + assert( stripNonDigit("\'13-=2 4kop") == "1324" ); +} + +/// Converts a word into a number, ignoring all non alpha characters +string wordToNum( in string word ) +{ +// translation table for the task at hand +const char[256] TRANSLATE = + " " // 0 + " 0123456789 " // 32 + " 57630499617851881234762239 " // 64 + " 57630499617851881234762239 " + " " + " " + " " + " "; + string ret; + foreach(c; cast(ubyte[])word) + if (TRANSLATE[c] != ' ') + ret ~= TRANSLATE[c]; + return ret; +} + +unittest { + // Test wordToNum using the table from the task description. + assert( "01112223334455666777888999" == + wordToNum("E | J N Q | R W X | D S Y | F T | A M | C I V | B K U | L O P | G H Z")); + assert( "01112223334455666777888999" == + wordToNum("e | j n q | r w x | d s y | f t | a m | c i v | b k u | l o p | g h z")); + assert( "0123456789" == + wordToNum("0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9")); +} + +void main( string[] args ) +{ + // This associative array maps a number to an array of words. + stringarray[string] num2words; + + foreach(string word; new BufferedFile("dictionary.txt" ) ) + num2words[ wordToNum(word) ] ~= word.dup; // must dup + + /// Finds all alternatives for the given number + /// (should have been stripped from non-digit characters) + stringarray _FindWords( string numbers, bool digitok ) + in { + assert(numbers.length > 0); + } + out(result) { + foreach (a; result) + assert( wordToNum(a) == numbers ); + } + body { + stringarray ret; + bool foundword = false; + for (uint t=1; t<=numbers.length; ++t) { + auto alternatives = numbers[0..t] in num2words; + if (!alternatives) + continue; + foundword = true; + if (numbers.length > t) { + // Combine all current alternatives with all alternatives + // of the rest (next piece can start with a digit) + foreach (a2; _FindWords( numbers[t..$], true ) ) + foreach(a1; *alternatives) + ret ~= a1 ~ " " ~ a2; + } + else + ret ~= *alternatives; // append these alternatives + } + // Try to keep 1 digit, only if we're allowed and no other + // alternatives were found + // Testing "ret.length" makes more sense than testing "foundword", + // but the other implementations seem to do just this. + if (digitok && !foundword) { //ret.length == 0 + if(numbers.length > 1) { + // Combine 1 digit with all altenatives from the rest + // (next piece can not start with a digit) + foreach (a; _FindWords( numbers[1..$], false ) ) + ret ~= numbers[0..1] ~ " " ~ a; + } + else + ret ~= numbers[0..1]; // just append this digit + } + return ret; + } + + /// (This function was inlined in the original program) + /// Finds all alternatives for the given phone number + /// Returns: array of strings + stringarray FindWords( string phone_number ) + { + if (!phone_number.length) + return null; + // Strip the non-digit characters from the phone number, and + // pass it to the recursive function (leading digit is allowed) + return _FindWords( stripNonDigit(phone_number), true ); + } + + // Read the phone numbers + foreach(string phone; new BufferedFile("input.txt" ) ) + foreach(alternative; FindWords( phone ) ) + writefln(phone, ": ", alternative ); +} + diff --git a/tests/run.py b/tests/run.py index 457a97a1..bd285c2b 100644 --- a/tests/run.py +++ b/tests/run.py @@ -76,8 +76,9 @@ def run_tests(with_coverage=False): WIDTH = 70 - print >>sys.stderr, ('Pygments Test Suite running %s, stand by...' % - (with_coverage and "with coverage analysis" or "")).center(WIDTH) + print >>sys.stderr, \ + ('Pygments Test Suite running %s, stand by...' % + (with_coverage and "with coverage analysis" or "")).center(WIDTH) print >>sys.stderr, ('(using Python %s)' % sys.version.split()[0]).center(WIDTH) print >>sys.stderr, '='*WIDTH diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py index e9583445..ed949a88 100644 --- a/tests/test_html_formatter.py +++ b/tests/test_html_formatter.py @@ -98,7 +98,7 @@ class HtmlFormatterTest(unittest.TestCase): fmt = HtmlFormatter() sd = fmt.get_style_defs() self.assert_(sd.startswith('.')) - + fmt = HtmlFormatter(cssclass='foo') sd = fmt.get_style_defs() self.assert_(sd.startswith('.foo')) diff --git a/tests/test_token.py b/tests/test_token.py index ac57126c..8cf779f7 100644 --- a/tests/test_token.py +++ b/tests/test_token.py @@ -46,6 +46,6 @@ class TokenTest(unittest.TestCase): except SystemExit: pass - + if __name__ == '__main__': unittest.main() diff --git a/tests/test_util.py b/tests/test_util.py index 737a40f4..4ea30400 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -18,7 +18,7 @@ class UtilTest(unittest.TestCase): def test_getoptions(self): raises = self.assertRaises equals = self.assertEquals - + equals(util.get_bool_opt({}, 'a', True), True) equals(util.get_bool_opt({}, 'a', 1), True) equals(util.get_bool_opt({}, 'a', 'true'), True) @@ -39,14 +39,14 @@ class UtilTest(unittest.TestCase): def f1(): """ docstring headline - + other text """ def f2(): """ docstring headline - + other text """ @@ -58,14 +58,15 @@ class UtilTest(unittest.TestCase): def analyse(text): return 0.5 analyse = util.make_analysator(analyse) - self.assertEquals(X.analyse(''), 0.5) + self.assertEquals(X.analyse(''), 0.5) def test_shebang_matches(self): self.assert_(util.shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?')) self.assert_(util.shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')) self.assert_(util.shebang_matches('#!/usr/bin/startsomethingwith python', r'python(2\.\d)?')) - self.assert_(util.shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?')) + self.assert_(util.shebang_matches('#!C:\\Python2.4\\Python.exe', + r'python(2\.\d)?')) self.failIf(util.shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')) self.failIf(util.shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')) @@ -84,6 +85,6 @@ class UtilTest(unittest.TestCase): '<?xml ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">')) self.assert_(util.looks_like_xml('<html xmlns>abc</html>')) self.failIf(util.looks_like_xml('<html>')) - + if __name__ == '__main__': unittest.main() |