예제 #1
0
    def testExternalTemplate(self):
        templates = textwrap.dedent('''\
            group T;
            expr(args, op) ::= <<
            [<args; separator={<op>}>]
            >>
            ''')

        group = stringtemplate3.StringTemplateGroup(file=StringIO(templates),
                                                    lexer='angle-bracket')

        grammar = textwrap.dedent(r'''grammar T2;
            options {
              language=Python;
              output=template;
            }
            a : r+=arg OP r+=arg
              -> expr(op={$OP.text}, args={$r})
            ;
            arg: ID -> template(t={$ID.text}) "<t>";
            
            ID : 'a'..'z'+;
            OP: '+';
            WS : (' '|'\n') {$channel=HIDDEN;} ;
            ''')

        found = self.execParser(grammar, 'a', "a + b", group)

        self.failUnlessEqual("[a+b]", found)
예제 #2
0
    def testIndirectTemplateConstructor(self):
        templates = textwrap.dedent('''\
            group T;
            expr(args, op) ::= <<
            [<args; separator={<op>}>]
            >>
            ''')

        group = stringtemplate3.StringTemplateGroup(file=StringIO(templates),
                                                    lexer='angle-bracket')

        grammar = textwrap.dedent(r'''grammar T;
            options {
              language=Python;
              output=template;
            }
            a: ID
              {
                $st = %({"expr"})(args={[1, 2, 3]}, op={"+"})
              }
            ;
            
            ID : 'a'..'z'+;
            WS : (' '|'\n') {$channel=HIDDEN;} ;
            ''')

        found = self.execParser(grammar, 'a', "abc", group)

        self.failUnlessEqual("[1+2+3]", found)
예제 #3
0
def main(infile, outfile, args):
    # Use the constructor that accepts a Reader
    group = stringtemplate.StringTemplateGroup(file=(open(infile, "r")))
    #StringTemplateGroup.registerGroupLoader(CommonGroupLoader(infile,ErrorManager.getStringTemplateErrorListener()))
    #group = StringTemplateGroup.loadGroup()
    t = group.getInstanceOf("template")
    t["args"] = args
    out = str(t)
    #print out
    writer = open(outfile, "w")
    writer.write(out)
    writer.close()
예제 #4
0
def initialize_stg(stgfile='iv.st'):
    ''' Load the STG backend and set gobal STG pointer '''
    global STG

    # Load the file containing a group of templates
    STG = stringtemplate3.StringTemplateGroup(file=open(stgfile))
예제 #5
0
파일: metac.py 프로젝트: tiance7/CardDoc
 def __init__(self, templateFile):
     self.templateFile = templateFile
     self.templates = stringtemplate3.StringTemplateGroup(
         file=open(self.templateFile, 'r'),
         lexer='default'
         )
예제 #6
0
import sys
import antlr3
import stringtemplate3
from CMinusParser import CMinusParser
from CMinusLexer import CMinusLexer

if len(sys.argv) == 2:
    templateFileName = "Java.stg"
    inputFileName = sys.argv[1]

elif len(sys.argv) == 3:
    templateFileName = sys.argv[1]
    inputFileName = sys.argv[2]

else:
    sys.stderr.write(repr(sys.argv) + '\n')
    sys.exit(1)
    
templates = stringtemplate3.StringTemplateGroup(
    file=open(templateFileName, 'r'),
    lexer='angle-bracket'
    )

cStream = antlr3.ANTLRFileStream(inputFileName)
lexer = CMinusLexer(cStream)
tStream = antlr3.CommonTokenStream(lexer)
parser = CMinusParser(tStream)
parser.templateLib = templates
r = parser.program()
print r.st.toString()