rust/hedgewars-server/src/server/haskell.rs
changeset 15801 f57a3d48072b
parent 15581 ab095fc0256c
equal deleted inserted replaced
15800:6af892a0a4b8 15801:f57a3d48072b
     1 use nom::{
     1 use nom::{
     2     branch::alt,
     2     branch::alt,
     3     bytes::complete::{escaped_transform, is_not, tag, take_while, take_while1},
     3     bytes::complete::{escaped_transform, is_not, tag, take_while, take_while1},
     4     character::{is_alphanumeric, is_digit, is_space},
     4     character::{is_alphanumeric, is_digit, is_space},
     5     combinator::{map, map_res},
     5     combinator::{map, map_res},
     6     multi::{many0, separated_list},
     6     multi::{many0, separated_list0},
     7     sequence::{delimited, pair, preceded, separated_pair, terminated},
     7     sequence::{delimited, pair, preceded, separated_pair, terminated},
     8     ExtendInto, IResult,
     8     ExtendInto, IResult,
     9 };
     9 };
    10 use std::{
    10 use std::{
    11     collections::HashMap,
    11     collections::HashMap,
   157 }
   157 }
   158 
   158 
   159 fn surrounded<'a, P, O>(
   159 fn surrounded<'a, P, O>(
   160     prefix: &'static str,
   160     prefix: &'static str,
   161     suffix: &'static str,
   161     suffix: &'static str,
   162     parser: P,
   162     mut parser: P,
   163 ) -> impl Fn(&'a [u8]) -> HaskellResult<'a, O>
   163 ) -> impl FnMut(&'a [u8]) -> HaskellResult<'a, O>
   164 where
   164 where
   165     P: Fn(&'a [u8]) -> HaskellResult<'a, O>,
   165     P: FnMut(&'a [u8]) -> HaskellResult<'a, O>,
   166 {
   166 {
   167     move |input| {
   167     move |input| {
   168         delimited(
   168         delimited(
   169             delimited(take_while(is_space), tag(prefix), take_while(is_space)),
   169             delimited(take_while(is_space), tag(prefix), take_while(is_space)),
   170             |i| parser(i),
   170             |i| parser(i),
   283     )(input)
   283     )(input)
   284 }
   284 }
   285 
   285 
   286 fn tuple(input: &[u8]) -> HaskellResult<HaskellValue> {
   286 fn tuple(input: &[u8]) -> HaskellResult<HaskellValue> {
   287     map(
   287     map(
   288         surrounded("(", ")", separated_list(comma, value)),
   288         surrounded("(", ")", separated_list0(comma, value)),
   289         HaskellValue::Tuple,
   289         HaskellValue::Tuple,
   290     )(input)
   290     )(input)
   291 }
   291 }
   292 
   292 
   293 fn list(input: &[u8]) -> HaskellResult<HaskellValue> {
   293 fn list(input: &[u8]) -> HaskellResult<HaskellValue> {
   294     map(
   294     map(
   295         surrounded("[", "]", separated_list(comma, value)),
   295         surrounded("[", "]", separated_list0(comma, value)),
   296         HaskellValue::List,
   296         HaskellValue::List,
   297     )(input)
   297     )(input)
   298 }
   298 }
   299 
   299 
   300 fn identifier(input: &[u8]) -> HaskellResult<String> {
   300 fn identifier(input: &[u8]) -> HaskellResult<String> {
   314 fn structure(input: &[u8]) -> HaskellResult<HaskellValue> {
   314 fn structure(input: &[u8]) -> HaskellResult<HaskellValue> {
   315     alt((
   315     alt((
   316         map(
   316         map(
   317             pair(
   317             pair(
   318                 identifier,
   318                 identifier,
   319                 surrounded("{", "}", separated_list(comma, named_field)),
   319                 surrounded("{", "}", separated_list0(comma, named_field)),
   320             ),
   320             ),
   321             |(name, mut fields)| HaskellValue::Struct {
   321             |(name, mut fields)| HaskellValue::Struct {
   322                 name,
   322                 name,
   323                 fields: fields.drain(..).collect(),
   323                 fields: fields.drain(..).collect(),
   324             },
   324             },