grammar.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import * as DomUtil from '../common/dom_util.js';
  2. import { Engine } from '../common/engine.js';
  3. import * as LocaleUtil from '../l10n/locale_util.js';
  4. import { LOCALE } from '../l10n/locale.js';
  5. export const ATTRIBUTE = 'grammar';
  6. export class Grammar {
  7. static getInstance() {
  8. Grammar.instance = Grammar.instance || new Grammar();
  9. return Grammar.instance;
  10. }
  11. static parseInput(grammar) {
  12. const attributes = {};
  13. const components = grammar.split(':');
  14. for (const component of components) {
  15. const comp = component.split('=');
  16. const key = comp[0].trim();
  17. if (comp[1]) {
  18. attributes[key] = comp[1].trim();
  19. continue;
  20. }
  21. key.match(/^!/)
  22. ? (attributes[key.slice(1)] = false)
  23. : (attributes[key] = true);
  24. }
  25. return attributes;
  26. }
  27. static parseState(stateStr) {
  28. const state = {};
  29. const corrections = stateStr.split(' ');
  30. for (const correction of corrections) {
  31. const corr = correction.split(':');
  32. const key = corr[0];
  33. const value = corr[1];
  34. state[key] = value ? value : true;
  35. }
  36. return state;
  37. }
  38. static translateString(text) {
  39. if (text.match(/:unit$/)) {
  40. return Grammar.translateUnit(text);
  41. }
  42. const engine = Engine.getInstance();
  43. let result = engine.evaluator(text, engine.dynamicCstr);
  44. result = result === null ? text : result;
  45. if (Grammar.getInstance().getParameter('plural')) {
  46. result = LOCALE.FUNCTIONS.plural(result);
  47. }
  48. return result;
  49. }
  50. static translateUnit(text) {
  51. text = Grammar.prepareUnit(text);
  52. const engine = Engine.getInstance();
  53. const plural = Grammar.getInstance().getParameter('plural');
  54. const strict = engine.strict;
  55. const baseCstr = `${engine.locale}.${engine.modality}.default`;
  56. engine.strict = true;
  57. let cstr;
  58. let result;
  59. if (plural) {
  60. cstr = engine.defaultParser.parse(baseCstr + '.plural');
  61. result = engine.evaluator(text, cstr);
  62. }
  63. if (result) {
  64. engine.strict = strict;
  65. return result;
  66. }
  67. cstr = engine.defaultParser.parse(baseCstr + '.default');
  68. result = engine.evaluator(text, cstr);
  69. engine.strict = strict;
  70. if (!result) {
  71. return Grammar.cleanUnit(text);
  72. }
  73. if (plural) {
  74. result = LOCALE.FUNCTIONS.plural(result);
  75. }
  76. return result;
  77. }
  78. static prepareUnit(text) {
  79. const match = text.match(/:unit$/);
  80. return match
  81. ? text.slice(0, match.index).replace(/\s+/g, ' ') +
  82. text.slice(match.index)
  83. : text;
  84. }
  85. static cleanUnit(text) {
  86. if (text.match(/:unit$/)) {
  87. return text.replace(/:unit$/, '');
  88. }
  89. return text;
  90. }
  91. clear() {
  92. this.parameters_ = {};
  93. this.stateStack_ = [];
  94. }
  95. setParameter(parameter, value) {
  96. const oldValue = this.parameters_[parameter];
  97. value
  98. ? (this.parameters_[parameter] = value)
  99. : delete this.parameters_[parameter];
  100. return oldValue;
  101. }
  102. getParameter(parameter) {
  103. return this.parameters_[parameter];
  104. }
  105. setCorrection(correction, func) {
  106. this.corrections_[correction] = func;
  107. }
  108. setPreprocessor(preprocessor, func) {
  109. this.preprocessors_[preprocessor] = func;
  110. }
  111. getCorrection(correction) {
  112. return this.corrections_[correction];
  113. }
  114. getState() {
  115. const pairs = [];
  116. for (const [key, val] of Object.entries(this.parameters_)) {
  117. pairs.push(typeof val === 'string' ? key + ':' + val : key);
  118. }
  119. return pairs.join(' ');
  120. }
  121. processSingles() {
  122. const assignment = {};
  123. for (const single of this.singles) {
  124. assignment[single] = false;
  125. }
  126. this.singles = [];
  127. this.pushState(assignment);
  128. }
  129. pushState(assignment) {
  130. for (let [key, value] of Object.entries(assignment)) {
  131. if (key.match(/^\?/)) {
  132. delete assignment[key];
  133. key = key.slice(1);
  134. this.singles.push(key);
  135. }
  136. assignment[key] = this.setParameter(key, value);
  137. }
  138. this.stateStack_.push(assignment);
  139. }
  140. popState() {
  141. const assignment = this.stateStack_.pop();
  142. for (const [key, val] of Object.entries(assignment)) {
  143. this.setParameter(key, val);
  144. }
  145. }
  146. setAttribute(node) {
  147. if (node && node.nodeType === DomUtil.NodeType.ELEMENT_NODE) {
  148. const state = this.getState();
  149. if (state) {
  150. node.setAttribute(ATTRIBUTE, state);
  151. }
  152. }
  153. }
  154. preprocess(text) {
  155. return this.runProcessors(text, this.preprocessors_);
  156. }
  157. correct(text) {
  158. return this.runProcessors(text, this.corrections_);
  159. }
  160. apply(text, opt_flags) {
  161. this.currentFlags = opt_flags || {};
  162. text =
  163. this.currentFlags.adjust || this.currentFlags.preprocess
  164. ? Grammar.getInstance().preprocess(text)
  165. : text;
  166. if (this.parameters_['translate'] || this.currentFlags.translate) {
  167. text = Grammar.translateString(text);
  168. }
  169. text =
  170. this.currentFlags.adjust || this.currentFlags.correct
  171. ? Grammar.getInstance().correct(text)
  172. : text;
  173. this.currentFlags = {};
  174. return text;
  175. }
  176. runProcessors(text, funcs) {
  177. for (const [key, val] of Object.entries(this.parameters_)) {
  178. const func = funcs[key];
  179. if (!func) {
  180. continue;
  181. }
  182. text = val === true ? func(text) : func(text, val);
  183. }
  184. return text;
  185. }
  186. constructor() {
  187. this.currentFlags = {};
  188. this.parameters_ = {};
  189. this.corrections_ = {};
  190. this.preprocessors_ = {};
  191. this.stateStack_ = [];
  192. this.singles = [];
  193. }
  194. }
  195. export function correctFont(text, correction) {
  196. if (!correction || !text) {
  197. return text;
  198. }
  199. const regexp = LOCALE.FUNCTIONS.fontRegexp(LocaleUtil.localFont(correction));
  200. return text.replace(regexp, '');
  201. }
  202. function correctCaps(text) {
  203. let cap = LOCALE.ALPHABETS.capPrefix[Engine.getInstance().domain];
  204. if (typeof cap === 'undefined') {
  205. cap = LOCALE.ALPHABETS.capPrefix['default'];
  206. }
  207. return correctFont(text, cap);
  208. }
  209. function addAnnotation(text, annotation) {
  210. return text + ':' + annotation;
  211. }
  212. function numbersToAlpha(text) {
  213. return text.match(/\d+/)
  214. ? LOCALE.NUMBERS.numberToWords(parseInt(text, 10))
  215. : text;
  216. }
  217. function noTranslateText(text) {
  218. if (text.match(new RegExp('^[' + LOCALE.MESSAGES.regexp.TEXT + ']+$'))) {
  219. Grammar.getInstance().currentFlags['translate'] = false;
  220. }
  221. return text;
  222. }
  223. Grammar.getInstance().setCorrection('localFont', LocaleUtil.localFont);
  224. Grammar.getInstance().setCorrection('localRole', LocaleUtil.localRole);
  225. Grammar.getInstance().setCorrection('localEnclose', LocaleUtil.localEnclose);
  226. Grammar.getInstance().setCorrection('ignoreFont', correctFont);
  227. Grammar.getInstance().setPreprocessor('annotation', addAnnotation);
  228. Grammar.getInstance().setPreprocessor('noTranslateText', noTranslateText);
  229. Grammar.getInstance().setCorrection('ignoreCaps', correctCaps);
  230. Grammar.getInstance().setPreprocessor('numbers2alpha', numbersToAlpha);