Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
6.9 kB · 254 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255<?php
/** * Generates valid context-free code for most programming languages that could * pass as C. Except for PHP. But includes Java (mostly). */abstract class PhutilCLikeCodeSnippetContextFreeGrammar extends PhutilCodeSnippetContextFreeGrammar {
protected function buildRuleSet() { return array( $this->getStmtTerminationGrammarSet(), $this->getVarNameGrammarSet(), $this->getNullExprGrammarSet(), $this->getNumberGrammarSet(), $this->getExprGrammarSet(), $this->getCondGrammarSet(), $this->getLoopGrammarSet(), $this->getStmtGrammarSet(), $this->getAssignmentGrammarSet(), $this->getArithExprGrammarSet(), $this->getBoolExprGrammarSet(), $this->getBoolValGrammarSet(), $this->getTernaryExprGrammarSet(),
$this->getFuncNameGrammarSet(), $this->getFuncCallGrammarSet(), $this->getFuncCallParamGrammarSet(), $this->getFuncDeclGrammarSet(), $this->getFuncParamGrammarSet(), $this->getFuncBodyGrammarSet(), $this->getFuncReturnGrammarSet(), ); }
protected function getStartGrammarSet() { $start_grammar = parent::getStartGrammarSet();
$start_grammar['start'][] = '[funcdecl]';
return $start_grammar; }
protected function getStmtTerminationGrammarSet() { return $this->buildGrammarSet('term', array(';')); }
protected function getFuncCallGrammarSet() { return $this->buildGrammarSet('funccall', array( '[funcname]([funccallparam])', )); }
protected function getFuncCallParamGrammarSet() { return $this->buildGrammarSet('funccallparam', array( '', '[expr]', '[expr], [expr]', )); }
protected function getFuncDeclGrammarSet() { return $this->buildGrammarSet('funcdecl', array( 'function [funcname]([funcparam]) '. '{[funcbody, indent, block, trim=right]}', )); }
protected function getFuncParamGrammarSet() { return $this->buildGrammarSet('funcparam', array( '', '[varname]', '[varname], [varname]', '[varname], [varname], [varname]', )); }
protected function getFuncBodyGrammarSet() { return $this->buildGrammarSet('funcbody', array( "[stmt]\n[stmt]\n[funcreturn]", "[stmt]\n[stmt]\n[stmt]\n[funcreturn]", "[stmt]\n[stmt]\n[stmt]\n[stmt]\n[funcreturn]", )); }
protected function getFuncReturnGrammarSet() { return $this->buildGrammarSet('funcreturn', array( 'return [expr][term]', '', )); }
// Not really C, but put it here because of the curly braces and mostly shared // among Java and PHP protected function getClassDeclGrammarSet() { return $this->buildGrammarSet('classdecl', array( '[classinheritancemod] class [classname] {[classbody, indent, block]}', 'class [classname] {[classbody, indent, block]}', )); }
protected function getClassNameGrammarSet() { return $this->buildGrammarSet('classname', array( 'MuffinHouse', 'MuffinReader', 'MuffinAwesomizer', 'SuperException', 'Librarian', 'Book', 'Ball', 'BallOfCode', 'AliceAndBobsSharedSecret', 'FileInputStream', 'FileOutputStream', 'BufferedReader', 'BufferedWriter', 'Cardigan', 'HouseOfCards', 'UmbrellaClass', 'GenericThing', )); }
protected function getClassBodyGrammarSet() { return $this->buildGrammarSet('classbody', array( '[methoddecl]', "[methoddecl]\n\n[methoddecl]", "[propdecl]\n[propdecl]\n\n[methoddecl]\n\n[methoddecl]", "[propdecl]\n[propdecl]\n[propdecl]\n\n[methoddecl]\n\n[methoddecl]". "\n\n[methoddecl]", )); }
protected function getVisibilityGrammarSet() { return $this->buildGrammarSet('visibility', array( 'private', 'protected', 'public', )); }
protected function getClassInheritanceModGrammarSet() { return $this->buildGrammarSet('classinheritancemod', array( 'final', 'abstract', )); }
// Keeping this separate so we won't give abstract methods a function body protected function getMethodInheritanceModGrammarSet() { return $this->buildGrammarSet('methodinheritancemod', array( 'final', )); }
protected function getMethodDeclGrammarSet() { return $this->buildGrammarSet('methoddecl', array( '[visibility] [methodfuncdecl]', '[visibility] [methodfuncdecl]', '[methodinheritancemod] [visibility] [methodfuncdecl]', '[abstractmethoddecl]', )); }
protected function getMethodFuncDeclGrammarSet() { return $this->buildGrammarSet('methodfuncdecl', array( 'function [funcname]([funcparam]) '. '{[methodbody, indent, block, trim=right]}', )); }
protected function getMethodBodyGrammarSet() { return $this->buildGrammarSet('methodbody', array( "[methodstmt]\n[methodbody]", "[methodstmt]\n[funcreturn]", )); }
protected function getMethodStmtGrammarSet() { $stmts = $this->getStmtGrammarSet();
return $this->buildGrammarSet('methodstmt', array_merge( $stmts['stmt'], array( '[methodcall][term]', ))); }
protected function getMethodCallGrammarSet() { // Java/JavaScript return $this->buildGrammarSet('methodcall', array( 'this.[funccall]', '[varname].[funccall]', '[classname].[funccall]', )); }
protected function getAbstractMethodDeclGrammarSet() { return $this->buildGrammarSet('abstractmethoddecl', array( 'abstract function [funcname]([funcparam])[term]', )); }
protected function getPropDeclGrammarSet() { return $this->buildGrammarSet('propdecl', array( '[visibility] [varname][term]', )); }
protected function getClassRuleSets() { return array( $this->getClassInheritanceModGrammarSet(), $this->getMethodInheritanceModGrammarSet(), $this->getClassDeclGrammarSet(), $this->getClassNameGrammarSet(), $this->getClassBodyGrammarSet(), $this->getMethodDeclGrammarSet(), $this->getMethodFuncDeclGrammarSet(), $this->getMethodBodyGrammarSet(), $this->getMethodStmtGrammarSet(), $this->getMethodCallGrammarSet(), $this->getAbstractMethodDeclGrammarSet(), $this->getPropDeclGrammarSet(), $this->getVisibilityGrammarSet(), ); }
public function generateClass() { $rules = array_merge($this->getRules(), $this->getClassRuleSets()); $rules['start'] = array('[classdecl]'); $count = 0; return $this->applyRules('[start]', $count, $rules); }
}