1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-16 16:30:09 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/Parser.js

150 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
2024-01-13 20:03:36 +00:00
* Copyright (c) 2010-2024 Tim Düsterhus.
2018-08-16 22:30:59 +00:00
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2024-06-15 19:56:58 +00:00
* Change Date: 2028-06-15
2018-08-16 22:30:59 +00:00
*
* On the date above, in accordance with the Business Source
* License, use of this software will be governed by version 2
* or later of the General Public License.
*/
2020-11-01 16:41:19 +00:00
define(['Bastelstu.be/parser-combinator'], function (parsec) {
'use strict'
2018-08-16 22:30:59 +00:00
const { C, F, N, X, parser, Streams } = parsec
const response = parsec.parsec.response
const peek = function (p) {
return new parser((input, index = 0) =>
2020-11-01 16:41:19 +00:00
p.parse(input, index).fold(
(accept) => response.accept(accept.value, accept.input, index, false),
(reject) => response.reject(input.location(reject.offset), false)
2018-08-16 22:30:59 +00:00
)
2020-11-01 16:41:19 +00:00
)
2018-08-16 22:30:59 +00:00
}
2020-11-01 16:41:19 +00:00
const Whitespace = F.satisfy((item) => /\s/.test(item))
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
const Rest = F.any.optrep().map((item) => item.join(''))
const Rest1 = F.any.rep().map((item) => item.join(''))
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
const AlnumTrigger = C.letter
.or(N.digit)
.rep()
.map((item) => item.join(''))
const SymbolicTrigger = F.not(C.letter.or(N.digit).or(Whitespace))
.rep()
.map((item) => item.join(''))
2018-08-16 22:30:59 +00:00
const Slash = C.char('/')
const Trigger = Slash.thenRight(
2020-11-01 16:41:19 +00:00
peek(Slash.map((item) => null))
.or(AlnumTrigger.thenLeft(Whitespace.rep().or(F.eos)))
.or(SymbolicTrigger.thenLeft(Whitespace.optrep()))
2018-08-16 22:30:59 +00:00
).or(F.returns(null))
const Command = Trigger.then(Rest)
const Quote = C.char('"')
const QuotedUsername = Quote.thenRight(
2020-11-01 16:41:19 +00:00
Quote.thenRight(Quote).or(F.not(Quote)).rep()
)
.thenLeft(Quote)
.map((item) => item.join(''))
2018-08-16 22:30:59 +00:00
const Comma = C.char(',')
2020-11-01 16:41:19 +00:00
const UnquotedUsername = F.not(Comma.or(Quote).or(Whitespace))
.then(
F.not(Comma.or(Whitespace))
.optrep()
.map((item) => item.join(''))
)
.map((item) => item.join(''))
2018-08-16 22:30:59 +00:00
const Username = QuotedUsername.or(UnquotedUsername)
2020-11-01 16:41:19 +00:00
const Decimal = (length) =>
N.digit.occurrence(length).map((item) => parseInt(item.join(''), 10))
2018-08-16 22:30:59 +00:00
const Hexadecimal = N.digit
.or(C.charIn('abcdefABCDEF'))
.rep()
2020-11-01 16:41:19 +00:00
.map((x) => x.join(''))
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
const RGBHex = C.char('#')
.opt()
2018-08-16 22:30:59 +00:00
.thenRight(
2020-11-01 16:41:19 +00:00
Hexadecimal.filter((x) => x.length === 3 || x.length === 6).map(
(item) => {
if (item.length === 3) {
item = `${item[0]}${item[0]}${item[1]}${item[1]}${item[2]}${item[2]}`
}
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
return item
}
)
)
.map((item) => `#${item}`)
2018-08-16 22:30:59 +00:00
const Dash = C.char('-')
2020-11-01 16:41:19 +00:00
const Datestring = Decimal(4)
.filter((item) => 2000 <= item && item <= 2030)
.thenLeft(Dash)
.then(Decimal(2).filter((item) => 1 <= item && item <= 12))
.thenLeft(Dash)
.then(Decimal(2).filter((item) => 1 <= item))
2018-08-16 22:30:59 +00:00
const Colon = C.char(':')
2020-11-01 16:41:19 +00:00
const Timestring = Decimal(2)
.filter((item) => 0 <= item && item <= 23)
.thenLeft(Colon)
.then(Decimal(2).filter((item) => 0 <= item && item <= 59))
.thenLeft(Colon)
.then(Decimal(2).filter((item) => 0 <= item && item <= 59))
const ISODate = Datestring.then(C.char('T').thenRight(Timestring).opt()).map(
function ([year, month, day, time]) {
const date = new Date()
date.setFullYear(year)
date.setMonth(month - 1)
date.setDate(day)
time.map(function ([hour, minute, second]) {
date.setHours(hour)
date.setMinutes(minute)
date.setSeconds(second)
})
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
return date
}
)
2018-08-16 22:30:59 +00:00
return {
Streams,
stream: Streams,
AlnumTrigger,
Colon,
Command,
Dash,
Datestring,
Decimal,
Hexadecimal,
ISODate,
Quote,
QuotedUsername,
RGBHex,
Rest,
Rest1,
Slash,
SymbolicTrigger,
Timestring,
Trigger,
UnquotedUsername,
Username,
Whitespace,
C,
F,
N,
X,
}
2020-11-01 16:41:19 +00:00
})