summaryrefslogtreecommitdiff
path: root/scss/src/block_locator.py
blob: d4ffd741b2ad89359f350be53a08c3ccccbfba24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/env python

## locate_blocks() needs heavy optimizations... is way too slow right now!
## Any suggestion from python wizards? :-)

import re
import sys
from datetime import datetime

from six.moves import xrange

import pstats
import cProfile
from cStringIO import StringIO
def profile(fn):
    def wrapper(*args, **kwargs):
        profiler = cProfile.Profile()
        stream = StringIO()
        profiler.enable()
        try:
            res = fn(*args, **kwargs)
        finally:
            profiler.disable()
            stats = pstats.Stats(profiler, stream=stream)
            stats.sort_stats('time')
            print >>stream, ""
            print >>stream, "=" * 100
            print >>stream, "Stats:"
            stats.print_stats()

            print >>stream, "=" * 100
            print >>stream, "Callers:"
            stats.print_callers()

            print >>stream, "=" * 100
            print >>stream, "Callees:"
            stats.print_callees()
            print >>sys.stderr, stream.getvalue()
            stream.close()
        return res
    return wrapper


DEBUG = False
################################################################################
# Helper functions


SEPARATOR = '\x00'
_nl_num_re = re.compile(r'\n.+' + SEPARATOR, re.MULTILINE)
_blocks_re = re.compile(r'[{},;()\'"\n]')


def load_string(codestr):
    """
    Add line numbers to the string using SEPARATOR as the separation between
    the line number and the line.
    """
    # Decorate lines with their line numbers and a delimiting NUL and remove empty lines
    codestr = '\n'.join(str(i + 1) + SEPARATOR + s for i, l in enumerate(codestr.splitlines()) for s in (l.strip(),) if s)

    return codestr


def _strip_selprop(selprop, lineno):
    # Get the line number of the selector or property and strip all other
    # line numbers that might still be there (from multiline selectors)
    _lineno, _sep, selprop = selprop.partition(SEPARATOR)
    if _sep == SEPARATOR:
        _lineno = _lineno.strip(' \t\n;')
        try:
            lineno = int(_lineno)
        except ValueError:
            pass
    else:
        selprop = _lineno
    selprop = _nl_num_re.sub('\n', selprop)
    selprop = selprop.strip()
    return selprop, lineno


def _strip(selprop):
    # Strip all line numbers, ignoring them in the way
    selprop, _ = _strip_selprop(selprop, None)
    return selprop


################################################################################
# Algorithm implemented in C (much slower here):

PAR = 0
INSTR = 1
DEPTH = 2
SKIP = 3
THIN = 4
INIT = 5
SAFE = 6
LOSE = 7
START = 8
END = 9
LINENO = 10
SELPROP = 11


def _start_string(codestr, ctx, i, c):
    if DEBUG: print "_start_string"
    # A string starts
    ctx[INSTR] = c
    return
    yield


def _end_string(codestr, ctx, i, c):
    if DEBUG: print "_end_string"
    # A string ends (FIXME: needs to accept escaped characters)
    ctx[INSTR] = None
    return
    yield


def _start_parenthesis(codestr, ctx, i, c):
    if DEBUG: print "_start_parenthesis"
    # parenthesis begins:
    ctx[PAR] += 1
    ctx[THIN] = None
    ctx[SAFE] = i + 1
    return
    yield


def _end_parenthesis(codestr, ctx, i, c):
    if DEBUG: print "_end_parenthesis"
    ctx[PAR] -= 1
    return
    yield


def _flush_properties(codestr, ctx, i, c):
    if DEBUG: print "_flush_properties"
    # Flush properties
    if ctx[LOSE] <= ctx[INIT]:
        _property, ctx[LINENO] = _strip_selprop(codestr[ctx[LOSE]:ctx[INIT]], ctx[LINENO])
        if _property:
            yield ctx[LINENO], _property, None
            ctx[SELPROP] = _property
        ctx[LOSE] = ctx[INIT]
    return
    yield


def _start_block1(codestr, ctx, i, c):
    if DEBUG: print "_start_block1"
    # Start level-1 block
    if i > 0 and codestr[i - 1] == '#':  # Do not process #{...} as blocks!
        ctx[SKIP] = True
    else:
        ctx[START] = i
        if ctx[THIN] is not None and _strip(codestr[ctx[THIN]:i]):
            ctx[INIT] = ctx[THIN]
        for y in _flush_properties(codestr, ctx, i, c):
            yield y
        ctx[THIN] = None
    ctx[DEPTH] += 1
    return
    yield


def _start_block(codestr, ctx, i, c):
    if DEBUG: print "_start_block"
    # Start blocks:
    ctx[DEPTH] += 1
    return
    yield


def _end_block1(codestr, ctx, i, c):
    if DEBUG: print "_end_block1"
    # End level-1 block:
    ctx[DEPTH] -= 1
    if not ctx[SKIP]:
        ctx[END] = i
        _selectors, ctx[LINENO] = _strip_selprop(codestr[ctx[INIT]:ctx[START]], ctx[LINENO])
        _codestr = codestr[ctx[START] + 1:ctx[END]]
        if _selectors:
            yield ctx[LINENO], _selectors, _codestr
            ctx[SELPROP] = _selectors
        ctx[INIT] = ctx[SAFE] = ctx[LOSE] = ctx[END] + 1
        ctx[THIN] = None
    ctx[SKIP] = False
    return
    yield


def _end_block(codestr, ctx, i, c):
    if DEBUG: print "_end_block"
    # Block ends:
    ctx[DEPTH] -= 1
    return
    yield


def _end_property(codestr, ctx, i, c):
    if DEBUG: print "_end_property"
    # End of property (or block):
    ctx[INIT] = i
    if ctx[LOSE] <= ctx[INIT]:
        _property, ctx[LINENO] = _strip_selprop(codestr[ctx[LOSE]:ctx[INIT]], ctx[LINENO])
        if _property:
            yield ctx[LINENO], _property, None
            ctx[SELPROP] = _property
        ctx[INIT] = ctx[SAFE] = ctx[LOSE] = i + 1
    ctx[THIN] = None
    return
    yield


def _mark_safe(codestr, ctx, i, c):
    if DEBUG: print "_mark_safe"
    # We are on a safe zone
    if ctx[THIN] is not None and _strip(codestr[ctx[THIN]:i]):
        ctx[INIT] = ctx[THIN]
    ctx[THIN] = None
    ctx[SAFE] = i + 1
    return
    yield


def _mark_thin(codestr, ctx, i, c):
    if DEBUG: print "_mark_thin"
    # Step on thin ice, if it breaks, it breaks here
    if ctx[THIN] is not None and _strip(codestr[ctx[THIN]:i]):
        ctx[INIT] = ctx[THIN]
        ctx[THIN] = i + 1
    elif ctx[THIN] is None and _strip(codestr[ctx[SAFE]:i]):
        ctx[THIN] = i + 1
    return
    yield


scss_function_map = {
    # (c, instr, par, depth)
    ('"', None, False, 0): _start_string,
    ("'", None, False, 0): _start_string,
    ('"', None, True, 0): _start_string,
    ("'", None, True, 0): _start_string,
    ('"', None, False, 1): _start_string,
    ("'", None, False, 1): _start_string,
    ('"', None, True, 1): _start_string,
    ("'", None, True, 1): _start_string,
    ('"', None, False, 2): _start_string,
    ("'", None, False, 2): _start_string,
    ('"', None, True, 2): _start_string,
    ("'", None, True, 2): _start_string,

    ('"', '"', False, 0): _end_string,
    ("'", "'", False, 0): _end_string,
    ('"', '"', True, 0): _end_string,
    ("'", "'", True, 0): _end_string,
    ('"', '"', False, 1): _end_string,
    ("'", "'", False, 1): _end_string,
    ('"', '"', True, 1): _end_string,
    ("'", "'", True, 1): _end_string,
    ('"', '"', False, 2): _end_string,
    ("'", "'", False, 2): _end_string,
    ('"', '"', True, 2): _end_string,
    ("'", "'", True, 2): _end_string,

    ("(", None, False, 0): _start_parenthesis,
    ("(", None, True, 0): _start_parenthesis,
    ("(", None, False, 1): _start_parenthesis,
    ("(", None, True, 1): _start_parenthesis,
    ("(", None, False, 2): _start_parenthesis,
    ("(", None, True, 2): _start_parenthesis,

    (")", None, True, 0): _end_parenthesis,
    (")", None, True, 1): _end_parenthesis,
    (")", None, True, 2): _end_parenthesis,

    ("{", None, False, 0): _start_block1,
    ("{", None, False, 1): _start_block,
    ("{", None, False, 2): _start_block,

    ("}", None, False, 1): _end_block1,
    ("}", None, False, 2): _end_block,

    (";", None, False, 0): _end_property,

    (",", None, False, 0): _mark_safe,

    ("\n", None, False, 0): _mark_thin,

    (None, None, False, 0): _flush_properties,
    (None, None, False, 1): _flush_properties,
    (None, None, False, 2): _flush_properties,
}


def _locate_blocks_a(codestr):
    """
    For processing CSS like strings.

    Either returns all selectors (that can be "smart" multi-lined, as
    long as it's joined by `,`, or enclosed in `(` and `)`) with its code block
    (the one between `{` and `}`, which can be nested), or the "lose" code
    (properties) that doesn't have any blocks.

    threshold is the number of blank lines before selectors are broken into
    pieces (properties).
    """
    ctx = [0, None, 0, False, None, 0, 0, 0, None, None, 0, '??']

    for m in _blocks_re.finditer(codestr):
        c = m.group()

        fn = scss_function_map.get((c, ctx[INSTR], ctx[PAR] != 0, 2 if ctx[DEPTH] > 1 else ctx[DEPTH]))
        if DEBUG: print fn and ' > ' or '   ', fn and fn.__name__, (c, ctx[INSTR], ctx[PAR] != 0, 2 if ctx[DEPTH] > 1 else ctx[DEPTH])
        if fn:
            for y in fn(codestr, ctx, m.start(), c):
                yield y

    codestr_end = len(codestr)
    exc = None
    if ctx[PAR]:
        exc = exc or "Missing closing parenthesis somewhere in block: '%s'" % ctx[SELPROP]
    elif ctx[INSTR]:
        exc = exc or "Missing closing string somewhere in block: '%s'" % ctx[SELPROP]
    elif ctx[DEPTH]:
        exc = exc or "Block never closed: '%s'" % ctx[SELPROP]
        while ctx[DEPTH] > 0 and ctx[INIT] < codestr_end:
            c = '}'
            fn = scss_function_map.get((c, ctx[INSTR], ctx[PAR] != 0, 2 if ctx[DEPTH] > 1 else ctx[DEPTH]))
            if DEBUG: print fn and ' > ' or ' ! ', fn and fn.__name__, (c, ctx[INSTR], ctx[PAR] != 0, 2 if ctx[DEPTH] > 1 else ctx[DEPTH])
            if fn:
                for y in fn(codestr, ctx, m.start(), c):
                    yield y

    if ctx[INIT] < codestr_end:
        ctx[INIT] = codestr_end
        c = None
        fn = scss_function_map.get((c, ctx[INSTR], ctx[PAR] != 0, 2 if ctx[DEPTH] > 1 else ctx[DEPTH]))
        if DEBUG: print fn and ' > ' or ' ! ', fn and fn.__name__, (c, ctx[INSTR], ctx[PAR] != 0, 2 if ctx[DEPTH] > 1 else ctx[DEPTH])
        if fn:
            for y in fn(codestr, ctx, m.start(), c):
                yield y

    if exc:
        raise Exception(exc)


################################################################################
# Algorithm using Regexps in pure Python (fastest pure python):


def _locate_blocks_b(codestr):
    """
    For processing CSS like strings.

    Either returns all selectors (that can be "smart" multi-lined, as
    long as it's joined by `,`, or enclosed in `(` and `)`) with its code block
    (the one between `{` and `}`, which can be nested), or the "lose" code
    (properties) that doesn't have any blocks.

    threshold is the number of blank lines before selectors are broken into
    pieces (properties).
    """
    lineno = 0

    par = 0
    instr = None
    depth = 0
    skip = False
    thin = None
    i = init = safe = lose = 0
    start = end = None

    for m in _blocks_re.finditer(codestr):
        i = m.start(0)
        c = codestr[i]
        if instr is not None:
            if c == instr:
                instr = None  # A string ends (FIXME: needs to accept escaped characters)
        elif c in ('"', "'"):
            instr = c  # A string starts
        elif c == '(':  # parenthesis begins:
            par += 1
            thin = None
            safe = i + 1
        elif c == ')':  # parenthesis ends:
            par -= 1
        elif not par and not instr:
            if c == '{':  # block begins:
                if depth == 0:
                    if i > 0 and codestr[i - 1] == '#':  # Do not process #{...} as blocks!
                        skip = True
                    else:
                        start = i
                        if thin is not None and _strip(codestr[thin:i]):
                            init = thin
                        if lose <= init:
                            _property, lineno = _strip_selprop(codestr[lose:init], lineno)
                            if _property:
                                yield lineno, _property, None
                            lose = init
                        thin = None
                depth += 1
            elif c == '}':  # block ends:
                if depth > 0:
                    depth -= 1
                    if depth == 0:
                        if not skip:
                            end = i
                            _selectors, lineno = _strip_selprop(codestr[init:start], lineno)
                            _codestr = codestr[start + 1:end].strip()
                            if _selectors:
                                yield lineno, _selectors, _codestr
                            init = safe = lose = end + 1
                            thin = None
                        skip = False
            elif depth == 0:
                if c == ';':  # End of property (or block):
                    init = i
                    if lose <= init:
                        _property, lineno = _strip_selprop(codestr[lose:init], lineno)
                        if _property:
                            yield lineno, _property, None
                        init = safe = lose = i + 1
                    thin = None
                elif c == ',':
                    if thin is not None and _strip(codestr[thin:i]):
                        init = thin
                    thin = None
                    safe = i + 1
                elif c == '\n':
                    if thin is not None and _strip(codestr[thin:i]):
                        init = thin
                        thin = i + 1
                    elif thin is None and _strip(codestr[safe:i]):
                        thin = i + 1  # Step on thin ice, if it breaks, it breaks here
    if depth > 0:
        if not skip:
            _selectors, lineno = _strip_selprop(codestr[init:start], lineno)
            _codestr = codestr[start + 1:].strip()
            if _selectors:
                yield lineno, _selectors, _codestr
            if par:
                raise Exception("Missing closing parenthesis somewhere in block: '%s'" % _selectors)
            elif instr:
                raise Exception("Missing closing string somewhere in block: '%s'" % _selectors)
            else:
                raise Exception("Block never closed: '%s'" % _selectors)
    losestr = codestr[lose:]
    for _property in losestr.split(';'):
        _property, lineno = _strip_selprop(_property, lineno)
        if _property:
            yield lineno, _property, None


################################################################################
# Algorithm implemented in C:


try:
    from _speedups import locate_blocks as _locate_blocks_c
except ImportError:
    _locate_blocks_c = None
    print >>sys.stderr, "Scanning acceleration disabled (_speedups not found)!"



################################################################################


codestr = """
simple {
    block;
}
#{ignored};
some,
selectors,
and multi-lined,
selectors
with more
{
    the block in here;
    can have, nested, selectors {
        and properties in nested blocks;
        and stuff with #{ ignored blocks };
    }
    properties-can: "have strings with stuff like this: }";
}
and other,
selectors
can be turned into "lose"
properties
if no commas are found
however this is a selector (
    as well as these things,
    which are parameters
    and can expand
    any number of
    lines) {
    and this is its block;;
}
"""
verify = '\t----------------------------------------------------------------------\n\t>[1] \'simple\'\n\t----------------------------------------------------------------------\n\t>\t[3] \'block\'\n\t----------------------------------------------------------------------\n\t>[5] \'#{ignored}\'\n\t----------------------------------------------------------------------\n\t>[6] \'some,\\nselectors,\\nand multi-lined,\\nselectors\'\n\t----------------------------------------------------------------------\n\t>[10] \'with more\'\n\t----------------------------------------------------------------------\n\t>\t[12] \'the block in here\'\n\t----------------------------------------------------------------------\n\t>\t[13] \'can have, nested, selectors\'\n\t----------------------------------------------------------------------\n\t>\t\t[14] \'and properties in nested blocks\'\n\t----------------------------------------------------------------------\n\t>\t\t[15] \'and stuff with #{ ignored blocks }\'\n\t----------------------------------------------------------------------\n\t>\t[17] \'properties-can: "have strings with stuff like this: }"\'\n\t----------------------------------------------------------------------\n\t>[19] \'and other,\\nselectors\\ncan be turned into "lose"\\nproperties\'\n\t----------------------------------------------------------------------\n\t>[23] \'if no commas are found\\nhowever this is a selector (\\nas well as these things,\\nwhich are parameters\\nand can expand\\nany number of\\nlines)\'\n\t----------------------------------------------------------------------\n\t>\t[30] \'and this is its block\'\n'


def process_block(locate_blocks, codestr, level=0, dump=False):
    ret = '' if dump else None
    for lineno, selprop, block in locate_blocks(codestr):
        if dump:
            ret += '\t%s\n\t>%s[%s] %s\n' % ('-' * 70, '\t' * level, lineno, repr(selprop))
        if block:
            _ret = process_block(locate_blocks, block, level + 1, dump)
            if dump:
                ret += _ret
    return ret


def process_blocks(locate_blocks, codestr):
    for q in xrange(20000):
        process_block(locate_blocks, codestr)
profiled_process_blocks = profile(process_blocks)

if __name__ == "__main__":
    codestr = load_string(codestr)

    for locate_blocks, desc in (
        (_locate_blocks_a, "Pure Python, Full algorithm (_locate_blocks_a)"),
        (_locate_blocks_b, "Pure Python, Condensed algorithm (_locate_blocks_b)"),
        (_locate_blocks_c, "Builtin C Function, Full algorithm (_locate_blocks_c)"),
    ):
        if locate_blocks:
            ret = process_block(locate_blocks, codestr, dump=True)
            # print "This is what %s returned:" % desc
            # print ret
            # print repr(ret)
            assert ret == verify, '\nFrom %s, got:\n%s\nShould be:\n%s' % (desc, ret, verify)

            start = datetime.now()
            print >>sys.stderr, "Timing: %s..." % desc,
            process_blocks(locate_blocks, codestr)
            elap = datetime.now() - start

            elapms = elap.seconds * 1000.0 + elap.microseconds / 1000.0
            print >>sys.stderr, "Done! took %06.3fms" % elapms