GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — CustomQueryLanguage/src/Transpiler/SuggestionManager.php at master · alorian/CustomQueryLanguage

Expand file tree

/

SuggestionManager.php

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
152 lines (134 loc) · 4.95 KB
/

SuggestionManager.php

Copy path

File metadata and controls

152 lines (134 loc) · 4.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace App\Transpiler;
use App\Exception\ParserUnexpectedTokenException;
use App\Lexer\KeywordToken\AndToken;
use App\Lexer\KeywordToken\ByToken;
use App\Lexer\KeywordToken\InToken;
use App\Lexer\KeywordToken\IsToken;
use App\Lexer\KeywordToken\NotToken;
use App\Lexer\KeywordToken\OrderToken;
use App\Lexer\KeywordToken\OrToken;
use App\Lexer\Lexer;
use App\Lexer\SimpleToken\CommaToken;
use App\Lexer\SimpleToken\DotToken;
use App\Lexer\SimpleToken\EqualToken;
use App\Lexer\SimpleToken\GreaterEqualToken;
use App\Lexer\SimpleToken\GreaterToken;
use App\Lexer\SimpleToken\LessEqualToken;
use App\Lexer\SimpleToken\LessToken;
use App\Lexer\SimpleToken\NotEqualToken;
use App\Lexer\SimpleToken\ParenLeftToken;
use App\Lexer\SimpleToken\ParenRightToken;
use App\Lexer\SimpleToken\PlusToken;
use App\Lexer\SimpleToken\StarToken;
use App\Lexer\SimpleToken\ContainToken;
use App\Lexer\TypeToken\EmptyToken;
use App\Lexer\TypeToken\FalseToken;
use App\Lexer\TypeToken\NullToken;
use App\Lexer\TypeToken\TrueToken;
use App\Lexer\TypeToken\UnexpectedToken;
use App\Parser\Node\FieldNode;
use App\Parser\Node\QueryNode;
use App\Parser\Parser;
class SuggestionManager
{
public function __construct(
protected Lexer $lexer,
protected Parser $parser,
protected FieldsCollection $fieldsCollection
) {
}
public function addSuggestions(CustomQueryState $queryState): void
{
if ($queryState->caretPos === 0) {
return;
}
// cutting initial request to the caret position
$rawQuery = mb_substr($queryState->getQuery(), 0, $queryState->caretPos);
$latestInput = null;
if (preg_match('#([\S]+)$#', $rawQuery, $matches) === 1) {
$latestInput = $matches[0];
$rawQuery = mb_substr($rawQuery, 0, -mb_strlen($latestInput));
}
try {
$tokensList = $this->lexer->analyze($rawQuery);
array_pop($tokensList);
$tokensList[] = new UnexpectedToken();
$this->parser->parse($tokensList);
} catch (ParserUnexpectedTokenException $exception) {
if ($exception->operationError !== null) {
$expectedInputStrings = $this->makeExpectedInputStrings(
$exception->operationError->expectedTokensList
);
$queryState->suggestionsList = $this->makeSuggestions($expectedInputStrings, $latestInput);
}
} catch (\Throwable $e) {
}
}
protected function makeSuggestions(array $stringsList, string $input = null): array
{
$suggestionsList = [];
if ($input !== null) {
foreach ($stringsList as $string) {
if ($string !== $input && mb_stripos($string, $input) === 0) {
$suggestion = [
'label' => $string,
'value' => mb_substr($string, mb_strlen($input))
];
$suggestionsList[] = $suggestion;
}
}
} else {
foreach ($stringsList as $string) {
$suggestion = [
'label' => $string,
'value' => $string
];
$suggestionsList[] = $suggestion;
}
}
return $suggestionsList;
}
protected function makeExpectedInputStrings(array $tokensList): array
{
$expectedStrings = [];
foreach ($tokensList as $tokenClass) {
switch ($tokenClass) {
case AndToken::class:
case ByToken::class:
case InToken::class:
case IsToken::class:
case NotToken::class:
case OrderToken::class:
case OrToken::class:
//case CommaToken::class:
case DotToken::class:
case EqualToken::class:
case GreaterEqualToken::class:
case GreaterToken::class:
case LessEqualToken::class:
case LessToken::class:
case NotEqualToken::class:
//case ParenLeftToken::class:
//case ParenRightToken::class:
case PlusToken::class:
case StarToken::class:
case ContainToken::class:
case EmptyToken::class:
case NullToken::class:
case FalseToken::class:
case TrueToken::class:
$expectedStrings[] = $tokenClass::LEXEME;
break;
case FieldNode::class:
$possibleFieldNames = $this->fieldsCollection->getPossibleNames();
asort($possibleFieldNames);
foreach ($this->fieldsCollection->getPossibleNames() as $fieldName) {
array_unshift($expectedStrings, $fieldName);
}
break;
}
}
return $expectedStrings;
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.