engine.js 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function annotateEngine(src, rules, ruleTypes, ruleMap) {
  2. var tokens = [];
  3. while(src) {
  4. // Pick rule
  5. var rule = ruleTypes.filter(function(ruleName, idx) {
  6. var regex = rules[ruleName];
  7. return regex.exec(src);
  8. })[0];
  9. // No matching rules
  10. if(!rule) {
  11. throw new Error('No rule found for: ' + src);
  12. }
  13. // Use rule to extract block
  14. var ruleRegex = rules[rule];
  15. var block = ruleRegex.exec(src);
  16. // Get rule type
  17. var type = ruleMap[rule] || rule;
  18. // Get raw text
  19. var raw = block[0];
  20. // Break out here to avoid infinite loops
  21. if(raw.length === 0) {
  22. break;
  23. }
  24. tokens.push({
  25. type: ruleMap[rule] || rule,
  26. raw: raw,
  27. matches: block.slice(1),
  28. });
  29. // Update source
  30. src = src.substring(raw.length);
  31. }
  32. return tokens;
  33. }
  34. module.exports = annotateEngine;