-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.pegjs
74 lines (63 loc) · 1.53 KB
/
grammar.pegjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This is the grammar that is used to generate the tokenParser.
// In order to use the parser, do the following:
// import tokenParser from './path/to/hammer/tokenParser';
// // Or if you're using this from outside of zeemee-messaging-client:
// import { tokenParser } from 'zeemee-messaging-client';
//
// To use:
// let myTokens = tokenParser.parse(myText);
// Test text:
//
// This is a long message. ::tcu::.
// Look at this cool photo! ::photoref1::
//
// Should produce this output:
//
// [
// {
// "type": "text",
// "value": "This is a long message. ",
// "originalText": "This is a long message. "
// },
// {
// "type": "token",
// "value": "tcu",
// "originalText": "::tcu::"
// },
// {
// "type": "text",
// "value": ".
// Look at this cool photo! ",
// "originalText": ".
// Look at this cool photo! "
// },
// {
// "type": "token",
// "value": "photoref1",
// "originalText": "::photoref1::"
// }
// ]
MessagePortions
= MessagePortion +
MessagePortion
= NonToken / Token
NonToken
= letters:Letter+ {
return {
type: 'text',
value: letters.join(''),
originalText: letters.join(''),
};
}
Letter
= ! Token letter:. { return letter }
Token
= "::" tokenName:TokenName "::" {
return {
type: 'token',
value: tokenName,
originalText: "::" + tokenName + "::",
};
}
TokenName
= name:[-_.+0-9a-zA-Z]+ { return name.join('') }