parsing/parser2.js

  1. 'use strict'
  2. const Tokenizer = require('../tokenizing/tokenizer')
  3. const CommandParsers = require('./commandParsers')
  4. /** */
  5. class CommandParser {
  6. constructor() {
  7. this._tokenizer = new Tokenizer()
  8. this._commandParsers = new CommandParsers()
  9. }
  10. /**
  11. * @param {String} line
  12. * @returns {boolean} true if the line can be parsed, false if not
  13. */
  14. isParseable(line) {
  15. return line.indexOf('@amy') >= 0
  16. }
  17. /**
  18. * @param {String} line the line to be parsed
  19. * @returns {Array<Parser.Command>} an array of commands
  20. */
  21. parseLine(line) {
  22. let operations = []
  23. let tokens = this._tokenizer.tokenize(line)
  24. if (tokens.length === 2) {
  25. tokens = tokens[1].match(/[a-zA-Z]+|\([\.a-zA-Z/]+\)/g)
  26. }
  27. tokens = tokens.map((token) => {
  28. return token.replace('(', '').replace(')', '')
  29. })
  30. tokens.forEach((token, index) => {
  31. const parser = this._commandParsers.parserFor(token)
  32. if (parser) {
  33. const operation = parser.parse(tokens, index)
  34. operations.push(operation)
  35. }
  36. })
  37. return operations
  38. }
  39. }
  40. module.exports = CommandParser