Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions kframework_ffi/src/kllvm/marshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum MarshalError {
Unsupported(&'static str),
UnknownVar(String),
Cstring,
InvalidString(String, usize),
}

pub trait VarHandler {
Expand Down Expand Up @@ -125,8 +126,8 @@ impl<H: VarHandler> Marshaller<H> {
value: &kore::Str,
) -> Result<(Pattern, bool), MarshalError> {
let s = build_sort(sort)?;
let pattern =
Pattern::new_token(value.to_kore().as_str(), &s).map_err(|_| MarshalError::Cstring)?;
let pattern = Pattern::new_token(&value.0, &s)
.map_err(|i| MarshalError::InvalidString(value.0.clone(), i))?;
Ok((pattern, true))
}

Expand Down
20 changes: 16 additions & 4 deletions kframework_ffi/src/kllvm/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::ffi;
use super::{Block, Sort, Symbol};
use libc;
use std::ffi::{c_void, CStr, CString, NulError};
use std::ffi::{c_void, CStr, CString};
use std::fmt;
use std::str::FromStr;

Expand Down Expand Up @@ -30,9 +30,21 @@ impl Pattern {
}

/// Build a fresh token (Dv) pattern of the given sort.
pub fn new_token(value: &str, sort: &Sort) -> Result<Self, NulError> {
let c_val = CString::new(value)?;
let raw = unsafe { ffi::kore_pattern_new_token(c_val.as_ptr(), sort.sort) };
/// Only supports strings that can be Latin-1 encoded.
/// On error, returns the index of the first character outside the Latin-1 range.
pub fn new_token(value: &str, sort: &Sort) -> Result<Self, usize> {
let bytes = value
.chars()
.enumerate()
.map(|(i, c)| u8::try_from(c as u32).map_err(|_| i))
.collect::<Result<Vec<_>, _>>()?;
let raw = unsafe {
ffi::kore_pattern_new_token_with_len(
bytes.as_ptr() as *const std::os::raw::c_char,
bytes.len(),
sort.sort,
)
};
Ok(Self { pattern: raw })
}

Expand Down
Loading