Path: blob/master/src/infrastructure/lipsum/code/PhutilCLikeCodeSnippetContextFreeGrammar.php
12262 views
<?php12/**3* Generates valid context-free code for most programming languages that could4* pass as C. Except for PHP. But includes Java (mostly).5*/6abstract class PhutilCLikeCodeSnippetContextFreeGrammar7extends PhutilCodeSnippetContextFreeGrammar {89protected function buildRuleSet() {10return array(11$this->getStmtTerminationGrammarSet(),12$this->getVarNameGrammarSet(),13$this->getNullExprGrammarSet(),14$this->getNumberGrammarSet(),15$this->getExprGrammarSet(),16$this->getCondGrammarSet(),17$this->getLoopGrammarSet(),18$this->getStmtGrammarSet(),19$this->getAssignmentGrammarSet(),20$this->getArithExprGrammarSet(),21$this->getBoolExprGrammarSet(),22$this->getBoolValGrammarSet(),23$this->getTernaryExprGrammarSet(),2425$this->getFuncNameGrammarSet(),26$this->getFuncCallGrammarSet(),27$this->getFuncCallParamGrammarSet(),28$this->getFuncDeclGrammarSet(),29$this->getFuncParamGrammarSet(),30$this->getFuncBodyGrammarSet(),31$this->getFuncReturnGrammarSet(),32);33}3435protected function getStartGrammarSet() {36$start_grammar = parent::getStartGrammarSet();3738$start_grammar['start'][] = '[funcdecl]';3940return $start_grammar;41}4243protected function getStmtTerminationGrammarSet() {44return $this->buildGrammarSet('term', array(';'));45}4647protected function getFuncCallGrammarSet() {48return $this->buildGrammarSet('funccall',49array(50'[funcname]([funccallparam])',51));52}5354protected function getFuncCallParamGrammarSet() {55return $this->buildGrammarSet('funccallparam',56array(57'',58'[expr]',59'[expr], [expr]',60));61}6263protected function getFuncDeclGrammarSet() {64return $this->buildGrammarSet('funcdecl',65array(66'function [funcname]([funcparam]) '.67'{[funcbody, indent, block, trim=right]}',68));69}7071protected function getFuncParamGrammarSet() {72return $this->buildGrammarSet('funcparam',73array(74'',75'[varname]',76'[varname], [varname]',77'[varname], [varname], [varname]',78));79}8081protected function getFuncBodyGrammarSet() {82return $this->buildGrammarSet('funcbody',83array(84"[stmt]\n[stmt]\n[funcreturn]",85"[stmt]\n[stmt]\n[stmt]\n[funcreturn]",86"[stmt]\n[stmt]\n[stmt]\n[stmt]\n[funcreturn]",87));88}8990protected function getFuncReturnGrammarSet() {91return $this->buildGrammarSet('funcreturn',92array(93'return [expr][term]',94'',95));96}9798// Not really C, but put it here because of the curly braces and mostly shared99// among Java and PHP100protected function getClassDeclGrammarSet() {101return $this->buildGrammarSet('classdecl',102array(103'[classinheritancemod] class [classname] {[classbody, indent, block]}',104'class [classname] {[classbody, indent, block]}',105));106}107108protected function getClassNameGrammarSet() {109return $this->buildGrammarSet('classname',110array(111'MuffinHouse',112'MuffinReader',113'MuffinAwesomizer',114'SuperException',115'Librarian',116'Book',117'Ball',118'BallOfCode',119'AliceAndBobsSharedSecret',120'FileInputStream',121'FileOutputStream',122'BufferedReader',123'BufferedWriter',124'Cardigan',125'HouseOfCards',126'UmbrellaClass',127'GenericThing',128));129}130131protected function getClassBodyGrammarSet() {132return $this->buildGrammarSet('classbody',133array(134'[methoddecl]',135"[methoddecl]\n\n[methoddecl]",136"[propdecl]\n[propdecl]\n\n[methoddecl]\n\n[methoddecl]",137"[propdecl]\n[propdecl]\n[propdecl]\n\n[methoddecl]\n\n[methoddecl]".138"\n\n[methoddecl]",139));140}141142protected function getVisibilityGrammarSet() {143return $this->buildGrammarSet('visibility',144array(145'private',146'protected',147'public',148));149}150151protected function getClassInheritanceModGrammarSet() {152return $this->buildGrammarSet('classinheritancemod',153array(154'final',155'abstract',156));157}158159// Keeping this separate so we won't give abstract methods a function body160protected function getMethodInheritanceModGrammarSet() {161return $this->buildGrammarSet('methodinheritancemod',162array(163'final',164));165}166167protected function getMethodDeclGrammarSet() {168return $this->buildGrammarSet('methoddecl',169array(170'[visibility] [methodfuncdecl]',171'[visibility] [methodfuncdecl]',172'[methodinheritancemod] [visibility] [methodfuncdecl]',173'[abstractmethoddecl]',174));175}176177protected function getMethodFuncDeclGrammarSet() {178return $this->buildGrammarSet('methodfuncdecl',179array(180'function [funcname]([funcparam]) '.181'{[methodbody, indent, block, trim=right]}',182));183}184185protected function getMethodBodyGrammarSet() {186return $this->buildGrammarSet('methodbody',187array(188"[methodstmt]\n[methodbody]",189"[methodstmt]\n[funcreturn]",190));191}192193protected function getMethodStmtGrammarSet() {194$stmts = $this->getStmtGrammarSet();195196return $this->buildGrammarSet('methodstmt',197array_merge(198$stmts['stmt'],199array(200'[methodcall][term]',201)));202}203204protected function getMethodCallGrammarSet() {205// Java/JavaScript206return $this->buildGrammarSet('methodcall',207array(208'this.[funccall]',209'[varname].[funccall]',210'[classname].[funccall]',211));212}213214protected function getAbstractMethodDeclGrammarSet() {215return $this->buildGrammarSet('abstractmethoddecl',216array(217'abstract function [funcname]([funcparam])[term]',218));219}220221protected function getPropDeclGrammarSet() {222return $this->buildGrammarSet('propdecl',223array(224'[visibility] [varname][term]',225));226}227228protected function getClassRuleSets() {229return array(230$this->getClassInheritanceModGrammarSet(),231$this->getMethodInheritanceModGrammarSet(),232$this->getClassDeclGrammarSet(),233$this->getClassNameGrammarSet(),234$this->getClassBodyGrammarSet(),235$this->getMethodDeclGrammarSet(),236$this->getMethodFuncDeclGrammarSet(),237$this->getMethodBodyGrammarSet(),238$this->getMethodStmtGrammarSet(),239$this->getMethodCallGrammarSet(),240$this->getAbstractMethodDeclGrammarSet(),241$this->getPropDeclGrammarSet(),242$this->getVisibilityGrammarSet(),243);244}245246public function generateClass() {247$rules = array_merge($this->getRules(), $this->getClassRuleSets());248$rules['start'] = array('[classdecl]');249$count = 0;250return $this->applyRules('[start]', $count, $rules);251}252253}254255256