本文整理匯總了TypeScript中@masala/parser.Streams類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Streams類的具體用法?TypeScript Streams怎麽用?TypeScript Streams使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Streams類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: day
import {Streams, F, C} from '@masala/parser'
import {assertFalse, assertTrue} from '../../assert';
function day() {
return C.stringIn(['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']);
}
function a(){
return C.char('a');
}
const string = '-MONDAY-';
function combinator() {
return F.any().then(day()).then(F.nop()).then(F.any()).thenEos();
}
let stream = Streams.ofString(string);
let parsing = combinator().parse(stream);
assertTrue(parsing.isAccepted());
assertTrue(parsing.isConsumed());
示例2:
import {Streams, F, C, N} from '@masala/parser'
import {assertEquals, assertArrayEquals, assertTrue} from '../../assert';
// Parsec needs a stream of characters
const document = '12';
const s = Streams.ofString(document);
// numberLitteral defines any int or float number
// We expect a number, then eos: End Of Stream
// We use drop() because we don't need the value of F.eos, we only want 12
const numberParser = N.numberLiteral().then(F.eos().drop());
const parsing = numberParser.parse(s);
// If the parser reached the end of stream (F.eos) without rejection, parsing is accepted
assertTrue(parsing.isAccepted());
// The parser has a 12 value inside the monoid
assertEquals (12, parsing.value);
示例3: assertArrayEquals
import {Streams, F, C, N} from '@masala/parser'
import {assertEquals, assertArrayEquals, assertTrue} from '../../assert';
const helloParser = C.string("Hello")
.then(C.char(' ').rep())
.then(C.char("'")).drop()
.then(C.letter().rep()) // keeping repeated ascii letters
.then(C.char("'").drop()); // keeping previous letters
const parsing = helloParser.parse(Streams.ofString("Hello 'World'"));
// C.letter.rep() will giv a array of letters
let x = parsing.value.array();
assertArrayEquals(['W','o','r','l','d'], parsing.value.array(), "Hello World joined");
// Note that helloParser will not reach the end of the stream; it will stop at the space after People
const peopleParsing = helloParser.parse(Streams.ofString("Hello 'People' in 2017"));
assertEquals("People", peopleParsing.value.join(''), "Hello People joined");
assertTrue(peopleParsing.offset < "Hello People in 2017".length, "Bad Offset for Hello People");
示例4: assertEquals
import {Streams, F, C, N} from '@masala/parser'
import {assertEquals, assertFalse, assertTrue} from '../../assert';
let response = C.char('a').rep().parse(Streams.ofString('aaaa'));
assertEquals(response.value.join(''), 'aaaa' );
assertEquals(response.offset, 4 )
assertTrue(response.isAccepted());
assertTrue(response.isConsumed());
// Partially accepted
response = C.char('a').rep().parse(Streams.ofString('aabb'));
assertEquals(response.value.join(''), 'aa' );
assertEquals(response.offset, 2 )
assertTrue(response.isAccepted());
assertFalse(response.isConsumed());
示例5: assertArrayEquals
import {C, F, Streams} from '@masala/parser'
import{assertArrayEquals, assertEquals, assertTrue} from './assert';
let stream = Streams.ofString('ab');
let parser = C.char('a');
let arrayParser = parser.then(C.char('b'));
let parsing = arrayParser.parse(stream);
assertArrayEquals(['a', 'b'], parsing.value) ; //compiling, types are almost OK
parser = C.char('a');
let charParsing = parser.parse(Streams.ofString('a'));
let charParsingValue = charParsing.value;
assertTrue(charParsingValue === 'a');
let singleParsing = parser.parse(stream);
assertTrue( 'a' === singleParsing.value); //compiling, types are almost OK
parser = C.char('a');
singleParsing = parser.parse(stream);
assertEquals( 'a' , singleParsing.value); //compiling, types are almost OK
示例6:
import {Streams, F, C, N} from '@masala/parser'
import {assertEquals} from '../../assert';
/**
* Created by Nicolas Zozol on 05/11/2017.
*/
const stream = Streams.ofString('abc');
const charsParser = C.char('a')
.then(C.char('b'))
.then(C.char('c'))
.then(F.eos().drop()); // End Of Stream ; droping its value, just checking it's here
let charsParsing = charsParser.parse(stream);
assertEquals('abc', charsParsing.value.join(''), 'Chars parsing');
示例7:
import {assertEquals, assertTrue} from '../../assert';
import {Streams, F, C, N} from '@masala/parser'
let stream= Streams.ofString('|4.6|');
const floorCombinator = C.char('|').drop()
.then(N.numberLiteral()) // we have ['|',4.6], we keep 4.6
.then(C.char('|').drop()) // we have [4.6, '|'], we keep 4.6
.map(x =>Math.floor(x));
// Parsec needs a stream of characters
let parsing = floorCombinator.parse(stream);
assertEquals( 4, parsing.value, 'Floor parsing');
示例8: A
import {Streams, F, C, Option, N, SingleParser, parserBuilder, ListParser} from '@masala/parser'
import {assertTrue} from '../../assert';
/**
* A gives its VALUE to B using flatMap
*/
function A(char){
return C.char(char.toUpperCase()).rep().flatMap(B);
}
/**
* There is recursion, and we call A with lazy. We send PARAMETERS to A
* within an array
*/
function B(aVal) {
return C.char('B').map(bVal=> aVal.join('')+'-'+bVal).or(F.lazy(A, ['a']));
}
const parser = A('a');
const str = 'AAAB';
const stream = Streams.ofString(str);
const parsing = parser.parse(stream);
assertTrue(parsing.offset === str.length);