functionalscript 0.0.474 → 0.0.475

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fsm/module.f.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  const list = require('../types/list/module.f.cjs')
2
2
  const { equal, isEmpty, fold, toArray, scan, foldScan } = list
3
3
  const byteSet = require('../types/byte_set/module.f.cjs')
4
- const { toRangeMap } = byteSet
4
+ const { toRangeMap, union: byteSetUnion, one, empty } = byteSet
5
5
  const sortedSet = require('../types/sorted_set/module.f.cjs')
6
- const { intersect, union } = sortedSet
6
+ const { intersect, union: sortedSetUnion } = sortedSet
7
7
  const rangeMap = require('../types/range_map/module.f.cjs')
8
8
  const { merge } = rangeMap
9
9
  const { unsafeCmp } = require('../types/function/compare/module.f.cjs')
@@ -11,6 +11,7 @@ const operator = require("../types/function/operator/module.f.cjs")
11
11
  const { strictEqual } = operator
12
12
  const { stringify } = require('../json/module.f.cjs')
13
13
  const { identity } = require('../types/function/module.f.cjs')
14
+ const { stringToList } = require('../text/utf16/module.f.cjs')
14
15
 
15
16
  /** @typedef {readonly[string, byteSet.ByteSet, string]} Rule */
16
17
 
@@ -24,8 +25,23 @@ const { identity } = require('../types/function/module.f.cjs')
24
25
 
25
26
  const stringifyIdentity = stringify(identity)
26
27
 
28
+ /** @type {(s: string) => byteSet.ByteSet} */
29
+ const toRange = s => {
30
+ const [b, e] = toArray(stringToList(s))
31
+ return byteSet.range([b, e])
32
+ }
33
+
34
+ /** @type {operator.Fold<number, byteSet.ByteSet>} */
35
+ const toUnionOp = i => bs => byteSetUnion(bs)(one(i))
36
+
37
+ /** @type {(s: string) => byteSet.ByteSet} */
38
+ const toUnion = s => {
39
+ const codePoints = stringToList(s)
40
+ return fold(toUnionOp)(empty)(codePoints)
41
+ }
42
+
27
43
  /** @type {rangeMap.Operators<sortedSet.SortedSet<string>>} */
28
- const mergeOp = { union: union(unsafeCmp), equal: equal(strictEqual) }
44
+ const mergeOp = { union: sortedSetUnion(unsafeCmp), equal: equal(strictEqual) }
29
45
 
30
46
  /** @type {(s: string) => (set: sortedSet.SortedSet<string>) => boolean} */
31
47
  const hasState = s => set => !isEmpty(intersect(unsafeCmp)([s])(set))
@@ -57,6 +73,11 @@ const addEntry = grammar => set => dfa => {
57
73
  return fold(addEntry(grammar))(newDfa)(newStates)
58
74
  }
59
75
 
76
+ /** @type {string[]} */
77
+ const emptyState = []
78
+
79
+ const emptyStateStringify = stringifyIdentity(emptyState)
80
+
60
81
  const initialState = ['']
61
82
 
62
83
  const initialStateStringify = stringifyIdentity(initialState)
@@ -65,7 +86,10 @@ const initialStateStringify = stringifyIdentity(initialState)
65
86
  const dfa = grammar => addEntry(grammar)(initialState)({})
66
87
 
67
88
  /** @type {(dfa: Dfa) => operator.Fold<number, string>} */
68
- const runOp = dfa => input => s => rangeMap.get(input)(dfa[s])
89
+ const runOp = dfa => input => s => {
90
+ const state = rangeMap.get(input)(dfa[s])
91
+ return state === undefined ? emptyStateStringify : state
92
+ }
69
93
 
70
94
  /** @type {(dfa: Dfa) => (input: list.List<number>) => list.List<string>} */
71
95
  const run = dfa => input => foldScan(runOp(dfa))(initialStateStringify)(input)
@@ -74,5 +98,9 @@ module.exports = {
74
98
  /** @readonly */
75
99
  dfa,
76
100
  /** @readonly */
77
- run
101
+ run,
102
+ /** @readonly */
103
+ toRange,
104
+ /** @readonly */
105
+ toUnion
78
106
  }
package/fsm/test.f.cjs CHANGED
@@ -4,21 +4,19 @@ const { sort, fromEntries } = require('../types/object/module.f.cjs')
4
4
  const json = require('../json/module.f.cjs')
5
5
  const { identity } = require('../types/function/module.f.cjs')
6
6
  const { toArray } = require('../types/list/module.f.cjs')
7
-
8
- /** @type {(c: string) => number} */
9
- const toCharCode = c => c.charCodeAt(0)
7
+ const { stringToList } = require('../text/utf16/module.f.cjs')
10
8
 
11
9
  const stringifyIdentity = json.stringify(identity)
12
10
 
13
11
  const buildDfa = () => {
14
- const lowercaseAlpha = byteSet.range([toCharCode('a'), toCharCode('z')])
15
- const uppercaseAlpha = byteSet.range([toCharCode('A'), toCharCode('Z')])
12
+ const lowercaseAlpha = _.toRange('az')
13
+ const uppercaseAlpha = _.toRange('AZ')
16
14
  const alpha = byteSet.union(lowercaseAlpha)(uppercaseAlpha)
17
- const idSymbol = byteSet.union(byteSet.one(toCharCode('_')))(byteSet.one(toCharCode('$')))
15
+ const idSymbol = _.toUnion('_$')
18
16
  const idBegin = byteSet.union(alpha)(idSymbol)
19
- const digit = byteSet.range([toCharCode('0'), toCharCode('9')])
17
+ const digit = _.toRange('09')
20
18
  const idNext = byteSet.union(idBegin)(digit)
21
- const dot = byteSet.one(toCharCode('.'))
19
+ const dot = _.toUnion('.')
22
20
 
23
21
  /** @type {_.Grammar} */
24
22
  const grammar = [
@@ -85,7 +83,7 @@ module.exports = {
85
83
  run: [
86
84
  () => {
87
85
  const dfa = buildDfa()
88
- const input = [toCharCode('a'), toCharCode('1')]
86
+ const input = stringToList('a1')
89
87
  const result = stringifyIdentity(toArray(_.run(dfa)(input)))
90
88
 
91
89
  const expectedOutput = [
@@ -97,7 +95,7 @@ module.exports = {
97
95
  },
98
96
  () => {
99
97
  const dfa = buildDfa()
100
- const input = [toCharCode('0'), toCharCode('.'), toCharCode('1')]
98
+ const input = stringToList('0.1')
101
99
  const result = stringifyIdentity(toArray(_.run(dfa)(input)))
102
100
 
103
101
  const expectedOutput = [
@@ -108,5 +106,29 @@ module.exports = {
108
106
  const expectedResult = stringifyIdentity(expectedOutput)
109
107
  if (result !== expectedResult) { throw result }
110
108
  },
109
+ () => {
110
+ const dfa = buildDfa()
111
+ const input = stringToList('//')
112
+ const result = stringifyIdentity(toArray(_.run(dfa)(input)))
113
+
114
+ const expectedOutput = [
115
+ '[]',
116
+ '[]'
117
+ ]
118
+ const expectedResult = stringifyIdentity(expectedOutput)
119
+ if (result !== expectedResult) { throw result }
120
+ },
121
+ () => {
122
+ const dfa = buildDfa()
123
+ const input = stringToList('::')
124
+ const result = stringifyIdentity(toArray(_.run(dfa)(input)))
125
+
126
+ const expectedOutput = [
127
+ '[]',
128
+ '[]'
129
+ ]
130
+ const expectedResult = stringifyIdentity(expectedOutput)
131
+ if (result !== expectedResult) { throw result }
132
+ }
111
133
  ]
112
134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.474",
3
+ "version": "0.0.475",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {