2

I have the following classes which will represent different kinds of data:

class A { ... }
class S { ... }
class F { ... }

Then I construct input array:

var input = [new F(), new A([11,22]), new S([33,44]), new F([55,66]), new A([77,88])]

Then I build a grammar for PEG.js:

start = (F A*)+
A = 'a' / S
S = 's'+ (A|F)
F = 'f'

I know that I may use PEG.js with a string as input (e.g. fasfa). But I see no way to use my own input stream of tokens. Is it possible at all?


Updated

The original problem can be summarizes as "I need a way to build toolbar one block at a time". Currently I have 3 types of blocks: Append, Squash, and Final.

Append - just appends current buttons into the resulting array
Squash - behaves like Append only if next block is Append (several
         Squash block melded into one)
Final  - just like Append, but if there is something next to it,
         it will start from the empty resulting array

Several examples:

Append([11,22])
    [11,22]

Append([11,22])
Squash([33,44])
    [11,22]

Append([11,22])
Squash([33,44])
Append([55,66])
    [11,22,33,44,55,66]

Append([11,22])
Squash([33,44])
Final([55,66])
    [11,22,33,44,55,66]

Append([11,22])
Squash([33,44])
Final([55,66])
Append([77,88])
    [77,88]

I thought that it is possible to think of this problem as of some language problem. That is I have an input stream of tokens which I need to convert to something other (e.g. some object).

vbarbarosh
  • 3,502
  • 4
  • 33
  • 43
  • Hello from Moldova. Can you please give a bit more details on your problem or what do you want to achieve? Do you need to update the Pegjs structure? – Dmitri Pavlutin Feb 19 '16 at 11:37
  • So you need a pegjs grammar, which from a series of append, squash and final actions will generate the final array? How many arguments append, squash and final accept (in your examples I see 2 params)? – Dmitri Pavlutin Feb 19 '16 at 12:43
  • Actually all of them (append, squash, and final) will take array of objects. Each object will represent one button. I've used numbers here for simplicity. (Already updated description - each block take array of ..., and result is also an array of ...) – vbarbarosh Feb 19 '16 at 12:50
  • I think it's not a problem to parse the array elements, but the question is that pegs result library will use integers, as being a textual processor. You mention that the input will be an array of objects. – Dmitri Pavlutin Feb 19 '16 at 13:18

0 Answers0