#!/usr/bin/env python3

import Parser, Lexer
from DSL import _lexerParser, _lexerLexer
from DSL import _parserLexer, _parserParser
from DSL import makeParser, makeLexer

lexerLexerConfig = r"""#dsl
    %keys ::= '%ignore' '%keys' '::='
    comment ::= /#[^\n]*\n/
    identifier ::= /[_a-zA-Z][_a-zA-Z0-9]*/
    sqString ::= /'[^']*'/
    dqString ::= /"[^"\\]*(\\.[^"\\]*)*"/
    reString ::= /\/[^\/\\]*(\\.[^\/\\]*)*\//
    %ignore ::= comment
"""
lexerLexer = makeLexer(lexerLexerConfig)

lexerParserConfig = r"""#dsl
    LexRules ::= rule*
    rule ::= identifier '::=' (sqString | dqString | reString)
           | '%keys' '::=' (sqString | dqString)+
           | '%ignore' '::=' (identifier | sqString | dqString)+
    %ignore ::= '::='
"""
lexerParser = makeParser(lexerParserConfig)

parserLexerConfig = r"""#dsl
    %keys ::= '$' '|' '::=' '(' ')' '*' '+' '?'
    identifier ::= /[_a-zA-Z][_a-zA-Z0-9]*/
    configType ::= /%(ignore|expandSingle|expand)/
    sqString ::= /'[^']*'/
    dqString ::= /"[^"\\]*(\\.[^"\\]*)*"/
    comment ::= /#[^\n]*\n/
    %ignore ::= comment
"""
parserLexer = makeLexer(parserLexerConfig)

parserParserConfig = r"""#dsl
    ParseRules ::= rule*
    rule ::= identifier '::=' alternate ('|' alternate)*
           | configType '::=' simpleItem+
    alternate ::= '$' | rhsItem+
    rhsItem ::= itemValue ('?' | '+' | '*')?
    itemValue ::= simpleItem | '(' alternate ('|' alternate)* ')'
    simpleItem ::= identifier | dqString | sqString
    %ignore ::= '::=' '|' '$' '(' ')'
    %expand ::= simpleItem
"""
parserParser = makeParser(parserParserConfig)

realOutput = _lexerParser.parse(_lexerLexer.parse(lexerLexerConfig))
testOutput = lexerParser.parse(lexerLexer.parse(lexerLexerConfig))
assert(str(realOutput) == str(testOutput))
realOutput = _lexerParser.parse(_lexerLexer.parse(parserLexerConfig))
testOutput = lexerParser.parse(lexerLexer.parse(parserLexerConfig))
assert(str(realOutput) == str(testOutput))

realOutput = _parserParser.parse(_parserLexer.parse(lexerParserConfig))
testOutput = parserParser.parse(parserLexer.parse(lexerParserConfig))
assert(str(realOutput) == str(testOutput))
realOutput = _parserParser.parse(_parserLexer.parse(parserParserConfig))
testOutput = parserParser.parse(parserLexer.parse(parserParserConfig))
assert(str(realOutput) == str(testOutput))

print('YA!')

@@ -349,32 +349,34 @@
 ::= /%22%5B%5E%22%5C%5C%5D*(%5C%5C
+%5C%5C
 .%5B%5E%22%5C%5C%5D*)*%22/%0A   
@@ -403,16 +403,18 @@
 /%5C%5C%5D*(%5C%5C
+%5C%5C
 .%5B%5E%5C/%5C%5C%5D
@@ -1019,16 +1019,18 @@
 %22%5C%5C%5D*(%5C%5C
+%5C%5C
 .%5B%5E%22%5C%5C%5D*

Fix bug of test.py due to change of Lexer's logic