@@ -6,6 +6,7 @@ import { ISyntaxTree } from 'core/syntax-tree/syntaxer';
6
6
const FIELD_REGEX = / ^ { ( ( [ ^ { } \\ ] | \\ .) + ) } / ;
7
7
const STRING_REGEX = / ^ ( ( ' ( [ ^ ' \\ ] | \\ .) * ' ) | ( " ( [ ^ " \\ ] | \\ .) * " ) | ( ` ( [ ^ ` \\ ] | \\ .) * ` ) ) / ;
8
8
const VALUE_REGEX = / ^ ( ( [ ^ \s ' " ` { } ( ) \\ ] | \\ .) + ) (?: [ \s ) ] | $ ) / ;
9
+ const PERMISSIVE_VALUE_REGEX = / ^ ( ( [ ^ ' " ` { } ( ) \\ ] | \\ .) + ) $ / ;
9
10
10
11
const getField = (
11
12
value : string
@@ -31,16 +32,6 @@ const getString = (
31
32
value : string
32
33
) => value . slice ( 1 , value . length - 1 ) . replace ( / \\ ( .) / g, '$1' ) ;
33
34
34
- const getValue = (
35
- value : string
36
- ) => {
37
- value = ( value . match ( VALUE_REGEX ) as any ) [ 1 ] ;
38
-
39
- return isNumeric ( value ) ?
40
- + value :
41
- value . replace ( / \\ ( .) / g, '$1' ) ;
42
- } ;
43
-
44
35
export const stringExpression : IUnboundedLexeme = {
45
36
present : ( tree : ISyntaxTree ) => getString ( tree . value ) ,
46
37
resolve : ( _target : any , tree : ISyntaxTree ) => {
@@ -55,17 +46,41 @@ export const stringExpression: IUnboundedLexeme = {
55
46
type : LexemeType . Expression
56
47
} ;
57
48
58
- export const valueExpression : IUnboundedLexeme = {
59
- present : ( tree : ISyntaxTree ) => getValue ( tree . value ) ,
60
- resolve : ( _target : any , tree : ISyntaxTree ) => {
61
- if ( VALUE_REGEX . test ( tree . value ) ) {
62
- return getValue ( tree . value ) ;
63
- } else {
64
- throw new Error ( ) ;
65
- }
66
- } ,
67
- regexp : VALUE_REGEX ,
68
- regexpMatch : 1 ,
69
- subType : 'value' ,
70
- type : LexemeType . Expression
71
- } ;
49
+ const getValueFactory = ( regex : RegExp ) => ( value : string ) => {
50
+ value = ( value . match ( regex ) as any ) [ 1 ] ;
51
+
52
+ return isNumeric ( value ) ?
53
+ + value :
54
+ value . replace ( / \\ ( .) / g, '$1' ) ;
55
+ } ;
56
+
57
+ const valueExpressionFactory = (
58
+ regex : RegExp ,
59
+ transform ?: ( v : any ) => any
60
+ ) : IUnboundedLexeme => {
61
+ const getValue = getValueFactory ( regex ) ;
62
+
63
+ return {
64
+ present : ( tree : ISyntaxTree ) => getValue ( tree . value ) ,
65
+ resolve : ( _target : any , tree : ISyntaxTree ) => {
66
+ if ( regex . test ( tree . value ) ) {
67
+ return getValue ( tree . value ) ;
68
+ } else {
69
+ throw new Error ( ) ;
70
+ }
71
+ } ,
72
+ regexp : regex ,
73
+ regexpMatch : 1 ,
74
+ subType : 'value' ,
75
+ transform,
76
+ type : LexemeType . Expression
77
+ } ;
78
+ } ;
79
+
80
+ export const valueExpression = valueExpressionFactory ( VALUE_REGEX ) ;
81
+ export const permissiveValueExpression = valueExpressionFactory (
82
+ PERMISSIVE_VALUE_REGEX ,
83
+ ( v : any ) => typeof v === 'string' && v . indexOf ( ' ' ) !== - 1 ?
84
+ `"${ v } "` :
85
+ v
86
+ ) ;
0 commit comments