@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 184 lines 4.9 kB view raw
1<?php 2 3final class PhutilJavaCodeSnippetContextFreeGrammar 4 extends PhutilCLikeCodeSnippetContextFreeGrammar { 5 6 protected function buildRuleSet() { 7 $parent_ruleset = parent::buildRuleSet(); 8 $rulesset = array_merge($parent_ruleset, $this->getClassRuleSets()); 9 10 $rulesset[] = $this->getTypeNameGrammarSet(); 11 $rulesset[] = $this->getNamespaceDeclGrammarSet(); 12 $rulesset[] = $this->getNamespaceNameGrammarSet(); 13 $rulesset[] = $this->getImportGrammarSet(); 14 $rulesset[] = $this->getMethodReturnTypeGrammarSet(); 15 $rulesset[] = $this->getMethodNameGrammarSet(); 16 $rulesset[] = $this->getVarDeclGrammarSet(); 17 $rulesset[] = $this->getClassDerivGrammarSet(); 18 19 return $rulesset; 20 } 21 22 protected function getStartGrammarSet() { 23 return $this->buildGrammarSet('start', 24 array( 25 '[import, block][nmspdecl, block][classdecl, block]', 26 )); 27 } 28 29 protected function getClassDeclGrammarSet() { 30 return $this->buildGrammarSet('classdecl', 31 array( 32 '[classinheritancemod] [visibility] class [classname][classderiv] '. 33 '{[classbody, indent, block]}', 34 '[visibility] class [classname][classderiv] '. 35 '{[classbody, indent, block]}', 36 )); 37 } 38 39 protected function getClassDerivGrammarSet() { 40 return $this->buildGrammarSet('classderiv', 41 array( 42 ' extends [classname]', 43 '', 44 '', 45 )); 46 } 47 48 protected function getTypeNameGrammarSet() { 49 return $this->buildGrammarSet('type', 50 array( 51 'int', 52 'boolean', 53 'char', 54 'short', 55 'long', 56 'float', 57 'double', 58 '[classname]', 59 '[type][]', 60 )); 61 } 62 63 protected function getMethodReturnTypeGrammarSet() { 64 return $this->buildGrammarSet('methodreturn', 65 array( 66 '[type]', 67 'void', 68 )); 69 } 70 71 protected function getNamespaceDeclGrammarSet() { 72 return $this->buildGrammarSet('nmspdecl', 73 array( 74 'package [nmspname][term]', 75 )); 76 } 77 78 protected function getNamespaceNameGrammarSet() { 79 return $this->buildGrammarSet('nmspname', 80 array( 81 'java.lang', 82 'java.io', 83 'com.example.proj.std', 84 'derp.example.www', 85 )); 86 } 87 88 protected function getImportGrammarSet() { 89 return $this->buildGrammarSet('import', 90 array( 91 'import [nmspname][term]', 92 'import [nmspname].*[term]', 93 'import [nmspname].[classname][term]', 94 )); 95 } 96 97 protected function getExprGrammarSet() { 98 $expr = parent::getExprGrammarSet(); 99 100 $expr['expr'][] = 'new [classname]([funccallparam])'; 101 102 $expr['expr'][] = '[methodcall]'; 103 $expr['expr'][] = '[methodcall]'; 104 $expr['expr'][] = '[methodcall]'; 105 $expr['expr'][] = '[methodcall]'; 106 107 // Add some 'char's 108 for ($ii = 0; $ii < 2; $ii++) { 109 $expr['expr'][] = "'".Filesystem::readRandomCharacters(1)."'"; 110 } 111 112 return $expr; 113 } 114 115 protected function getStmtGrammarSet() { 116 $stmt = parent::getStmtGrammarSet(); 117 118 $stmt['stmt'][] = '[vardecl]'; 119 $stmt['stmt'][] = '[vardecl]'; 120 // `try` to `throw` a `Ball`! 121 $stmt['stmt'][] = 'throw [classname][term]'; 122 123 return $stmt; 124 } 125 126 protected function getPropDeclGrammarSet() { 127 return $this->buildGrammarSet('propdecl', 128 array( 129 '[visibility] [type] [varname][term]', 130 )); 131 } 132 133 protected function getVarDeclGrammarSet() { 134 return $this->buildGrammarSet('vardecl', 135 array( 136 '[type] [varname][term]', 137 '[type] [assignment][term]', 138 )); 139 } 140 141 protected function getFuncNameGrammarSet() { 142 return $this->buildGrammarSet('funcname', 143 array( 144 '[methodname]', 145 '[classname].[methodname]', 146 // This is just silly (too much recursion) 147 // '[classname].[funcname]', 148 // Don't do this for now, it just clutters up output (thanks to rec.) 149 // '[nmspname].[classname].[methodname]', 150 )); 151 } 152 153 // Renamed from `funcname` 154 protected function getMethodNameGrammarSet() { 155 $funcnames = head(parent::getFuncNameGrammarSet()); 156 return $this->buildGrammarSet('methodname', $funcnames); 157 } 158 159 protected function getMethodFuncDeclGrammarSet() { 160 return $this->buildGrammarSet('methodfuncdecl', 161 array( 162 '[methodreturn] [methodname]([funcparam]) '. 163 '{[methodbody, indent, block, trim=right]}', 164 )); 165 } 166 167 protected function getFuncParamGrammarSet() { 168 return $this->buildGrammarSet('funcparam', 169 array( 170 '', 171 '[type] [varname]', 172 '[type] [varname], [type] [varname]', 173 '[type] [varname], [type] [varname], [type] [varname]', 174 )); 175 } 176 177 protected function getAbstractMethodDeclGrammarSet() { 178 return $this->buildGrammarSet('abstractmethoddecl', 179 array( 180 'abstract [methodreturn] [methodname]([funcparam])[term]', 181 )); 182 } 183 184}