#!/usr/bin/env python3

import re

source = r'''if x < y { v = 1 }'''

SPEC = r'''
    (?P<IDENT>  [_a-zA-Z] [_a-zA-Z0-9]* ) |
    (?P<NUMBER> [-]? [1-9] [0-9]*       ) |
    (?P<LT>     <                       ) |
    (?P<EQ>     =                       ) |
    (?P<LBRACE> {                       ) |
    (?P<RBRACE> }                       ) |
    (?P<WS>     \s+                     )
    '''

get_token = re.compile(SPEC, re.VERBOSE).match
keywords = { 'if' }

while len(source) > 0:
    m = get_token(source)
    if m is None:
        raise Exception(f'Unable to scan: {source}')

    start, stop = m.span()
    kind = m.lastgroup
    val = m.group(kind)

    # Find reserved keyword identifiers
    if kind == 'IDENT' and val in keywords:
        kind = val.upper()
        val = ''

    if kind != 'WS':
        print(f'{kind:6} {val}')

    source = source[stop:]
